PHP 8.5.0 Alpha 1 available for testing

Voting

: four minus three?
(Example: nine)

The Note You're Voting On

viundan at gmail dot com
8 years ago
Decision to avoid problem "it might replace a previously inserted value when doing multiple replacements. See also the examples in this document."

$urls - array of urls i want to replace with tag <a> and urls could be similar
http://abc.com/parameter/
http://abc.com/

// at first sort by length to have longest firstly
usort($urls,'sortByLen');

$replaces=[];

// replace all urls with unique
foreach($urls as $url){
$replace = '__REPLACE' . uniqid() . '__';
$text = str_replace($url,$replace, $text);
$replaces[$replace] = '<a href="' . $url . '">' . $url . '</a>';
}

foreach($replaces as $key => $replace){
$text = str_replace($key,$replace, $text);
}

--------------

function sortByLen($a,$b){
return strlen($b)-strlen($a);
}

Hope it will help others like me

<< Back to user notes page

To Top