Open In App

PHP | highlight_string() function

Last Updated : 06 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The highlight_string() function is an inbuilt function in PHP which is used to highlight the text string. It returns output in HTML text format.

Syntax:

highlight_string( $string, $return )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $string: It is the required parameter which specifies the text to be highlighted.
  • $return: It is the optional parameter. If it is True then it provides a highlighted code as a string, instead of printing it on the screen.

Return Value: If it is True then it returns highlighted code as a string, otherwise false on failure.

Below programs illustrate the highlight_string() function in PHP.

Program 1:




<?php
highlight_string('Welcome to Geeksforgeeks');
?>


Output:

<code><span style="color: #000000">
<span style="color: #0000BB"><?php phpinfo</span><span style="color: #007700">();
 </span><span style="color: #0000BB">?></span>
</span></code>

Program 2:




<?php
highlight_string('<?php
// PHP Program to add two numbers
  
function Add( $x, $y)
{
    $sum = $x + $y
      
    return $sum;
}
  
    // Driver Code
    echo Add(15, 32);
?>')
?>


Output:

<code><span style="color: #000000">
<span style="color: #0000BB">
<?php<br /></span><span style="color: #FF8000">
// PHP Program to add two numbers<br /><br />
</span><span style="color: #007700">function </span><span style="color: #0000BB">
Add</span><span style="color: #007700">( </span><span style="color: #0000BB">
$x</span><span style="color: #007700">, </span><span style="color: #0000BB">
$y</span><span style="color: #007700">)<br />{<br />    </span>
<span style="color: #0000BB">$sum </span><span style="color: #007700">= </span>
<span style="color: #0000BB">$x </span><span style="color: #007700">+ </span>
<span style="color: #0000BB">$y</span><span style="color: #007700">; <br />    <br />
    return </span><span style="color: #0000BB">$sum</span><span style="color: #007700">;<br />
}<br /><br />    </span><span style="color: #FF8000">
// Driver Code<br />    </span><span style="color: #007700">echo </span>
<span style="color: #0000BB">Add</span><span style="color: #007700">
(</span><span style="color: #0000BB">15</span><span style="color: #007700">
, </span><span style="color: #0000BB">32</span><span style="color: #007700">
);<br /></span><span style="color: #0000BB">?></span>
</span></code>

Reference: http://php.net/manual/en/function.highlight-string.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads