html - Emoticons replacement in PHP with some conditions -


this question has answer here:

i'm looking way replace emoticons in php , code below.

function emotify($text) {     $icons = array(         '3:)'   =>  '<li class="emoti emoti55"></li>',         'o:)'   =>  '<li class="emoti emoti54"></li>',         ':)'   =>  '<li class="emoti emoti00"></li>',         '>:('   =>  '<li class="emoti emoti19"></li>',         ':('   =>  '<li class="emoti emoti01"></li>',         ':p'   =>  '<li class="emoti emoti14"></li>',         '=d'   =>  '<li class="emoti emoti08"></li>',         '>:o'   =>  '<li class="emoti emoti18"></li>',         ':o'   =>  '<li class="emoti emoti15"></li>',         ';)'   =>  '<li class="emoti emoti04"></li>',         ':/'   =>  '<li class="emoti emoti03"></li>',         ':\'('   =>  '<li class="emoti emoti05"></li>',         '^_^'   =>  '<li class="emoti emoti18"></li>',         'b|'   =>  '<li class="emoti emoti09"></li>',         '<3'   =>  '<li class="emoti emoti65"></li>',         '-_-'   =>  '<li class="emoti emoti40"></li>',         'o.o'   =>  '<li class="emoti emoti10"></li>',         '(y)'   =>  '<li class="emoti emoti81"></li>',         );     return str_replace(array_keys($icons), array_values($icons), $text); }  //test work echo emotify(":) :( :p =d :o ;) :v >:( :/ :'( ^_^ 8-) b| <3 3:) o:) -_- o.o >:o :3 (y) "); 

i want if there string concatenate left or right sides of emoticon codes , don't replace it. example:

http://www.google.com   aaa:)bbb   :)111111   22222:)

i think may done using preg replace(?) please help, many thanks.

if want preserve speed advantage of strtr (that fastest way translate literal strings (the string parsed once key/values)), can proceed in 3 passes.

the first pass consists replace want protect placeholder. example:

$protected = array('http://'  => '#!#0#!#',                    'https://' => '#!#1#!#',                    'ftp://'   => '#!#2#!#', // etc. ); $str = strtr($str, $protected); 

note build of $protected can automated array('http://', 'https://', 'ftp://', ...);

second pass, use array:

$str = strtr($str, $icons); 

third pass, replace placeholders:

$str = strtr($str, array_flip($protected)); 

even if need 3 passes it, result far faster using preg_replace parse string once each key/value.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -