Changing links in a string using PHP
I am trying to tag links in a line by adding a redirect. The data is output from the MySQL database in string format and looks something like this:
$string = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
I am using the strpos function along with the "http" needle to get the location of all links in a string and store them in an array called positions. The array is filled with the character that the link starts with and looks like this:
Array
(
[0] => 12
[1] => 100
)
Then I loop over the array of positions and use substr_replace to add a redirect link to http. However, this only works for one link, and if I have multiple links in a line, they will be overwritten. Anyone have a clever solution?
Here is my code:
function stringInsert($str,$pos,$insertstr)
{
if (!is_array($pos))
$pos=array($pos);
$offset=-1;
foreach($pos as $p)
{
$offset++;
$str = substr($str, 0, $p+$offset) . $insertstr . substr($str, $p+$offset);
}
return $str;
}
$string = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
$needle = "http";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($string, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
$str_to_insert = "http://redirect.com?link=";
foreach ($positions as $value) {
$finalstring = substr_replace($string, $str_to_insert, $value, 0);
}
The end result should look like this:
$string = "<p><a href='http://redirect.com?link=http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://redirect.com?link=http://facebook.com'>Friend on Facebook</a></p>";
source to share
I think a more convenient solution might be to use str_replace ( http://php.net/manual/en/function.str-replace.php )
do something like this:
$string = str_replace(
['http://','https://'],
['http://redirect.com?link=http://', 'http://redirect.com?link=https://'],
$sourceString
);
source to share
I would go with a regex:
$str = "<p><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";
$str = preg_replace('/([\'\"])(http[^\'\"]+)([\'\"])/', '$1http://redirect.com?link=$2$3', $str);
echo htmlspecialchars($str);
The output I get is: <p><a href='http://redirect.com?link=http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://redirect.com?link=http://facebook.com'>Friend on Facebook</a></p>
Be aware that the last line is for display purposes only. Escaped html is used so you can see the result. You don't need a call to reference the actual work htmlspecialchars
.
source to share
Instead, let's use the DOMDocument parser, which allows us to use normalized methods for HTML strings.
$doc = new DOMDocument();
$doc->loadHTML($string);
Now we can iterate over all the binding elements and make the necessary changes:
foreach( $doc->getElementsByTagName("a") as $anchor) {
$newLink = "http://example.com";
$anchor->setAttribute('href', $newLink );
}
Then you can execute echo $doc->saveHTML();
when done.
You can also do your conditional comparisons within the actual foreach loop.
source to share
JQuery way to do it:
Html
<p id="addRedirect"><a href='http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>
JQuery code
$('#addRedirect > a').each(function(){
$(this).attr('href','http://redirect.com?link=' + $(this).attr('href'));
});
source to share