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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s