update page now

Voting

: max(eight, eight)?
(Example: nine)

The Note You're Voting On

viundan at gmail dot com
9 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