Category: PHP String Functions Intermediate

PHP str_replace() Function Example

PHP Script:


<?php

//Converting relative urls into absolute urls | PHP Tutors

$base_url = 'https://www.facebook.com/';
$anchors[0] = '<a href="tutorscommunity/">PHP Tutors Facebook Page Link </a>';
$anchors[1] = '<a href="groups/ptutors/">PHP Tutors Facebook Group Link </a>';

foreach($anchors as $val)
{
if(strpos($val,$base_url) === false)
{
echo str_replace('href="','href="'.$base_url,$val)."<br>";
}
else
{
echo $val."<br>";
}
}
?>

Output:

PHP Tutors Facebook Page Link
PHP Tutors Facebook Group Link

Description:

str_replace() function replaces the specified string with other specified string with in a string.

str_replace() function parameters:

1- Find_string: Specify the string which is going to be replace
2- Replace_string: Specify the string which replace the above find_string in searched_string
3- Searched_String: Specify the string which is going to be searched

PHP md5() Function Example

PHP Script:

<?php
$str=”PHP TUTORS md5 FUNCTION EXAMPLE”;
echo md5($str);
?>

Output:

cb97a255c33139010701c7a68c05d689

Description:

PHP md5() function calculates the MD5 hash of a string.
This function returns the calculated MD5 hash on success, or FALSE on failure.
It can be used to store password in database as encrypted form [md5 hash].

PHP md5 function parameters:

1- String: String for which md5 hash is going to be calculated.
2- Bolean value [optional]:
TRUE is for Raw 16 character binary format.
FALSE is for 32 character hex number.
By default value is FALSE.

PHP html_entity_decode() Function Example

PHP Script:

<?php
$str = “A ‘PHPTUTORS’ is &lt;b&gt;Awsome&lt;/b&gt;”;
echo html_entity_decode($str);
echo “< br/ >”;
echo html_entity_decode($str, ENT_QUOTES);
?>

Output:

A ‘PHPTUTORS’ is Awsome< br/ >A ‘PHPTUTORS’ is Awsome

Description:

html_entity_decode() function Convert all HTML entities to their applicable characters. In above example the source of the output will look like

[

A 'PHPTUTORS' is <b>Awsome</b>< br/ >A 'PHPTUTORS' is <b>Awsome</b>

]

html_entity_decode() function parameters:

1- string : The input string value.
2- int : Bitmask  for specifying criteria of encodings. [Optional] Default value : [ int $flags = ENT_COMPAT | ENT_HTML401 ]
3- string : Defines encoding used in conversion.  [Optional] Default value : [ string $encoding = ‘UTF-8’ ]

PHP htmlentities() Function Example

PHP Script:

<?php
$str = “A ‘PHPTUTORS’ is <b>Awsome</b>”;
echo htmlentities($str);
echo “< br/ >”;
echo htmlentities($str, ENT_QUOTES);
?>

Output:

A ‘PHPTUTORS’ is <b>Awsome</b>< br/ >A ‘PHPTUTORS’ is <b>Awsome</b>

Description:

htmlentities() function convert all applicable characters to HTML entities.

In above example the html source will look like

[

A 'PHPTUTORS' is &lt;b&gt;Awsome&lt;/b&gt;< br/ >A 'PHPTUTORS' is &lt;b&gt;Awsome&lt;/b&gt;

]

htmlentities() function parameters:

1- string : The input string value.
2- int : Bitmask  for specifying criteria of encodings. [Optional] Default value : [ int $flags = ENT_COMPAT | ENT_HTML401 ]
3- string : Defines encoding used in conversion.  [Optional] Default value : [ string $encoding = ‘UTF-8’ ]
4- bool : When this is false, PHP will not encode existing html entities. [Optional] Default value : [bool $double_encode = true]