preg_replace () replace only the first result why?

function filter($data) { $oldString = array('😀','😃','😄'); $newString = array('smile1','smile2','smile3'); return str_replace($oldString, $newString, $data); } function smile($data) { $data = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) { $char = current($m); $utf = iconv('UTF-8', 'UCS-4', $char); return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0")); }, $data); preg_match_all("/(&#[a-zA-Z0-9]{6};)/", $data, $hashtweet); foreach ($hashtweet[1] as $ht){ $data = preg_replace("/(&#[a-zA-Z0-9]{6};)/", filter(strtolower($ht)), $data); } $data = mb_convert_encoding($data, 'UTF-8', 'HTML-ENTITIES'); return $data; } echo smile('😀 😃 😄'); 

Here is the result.

smile1 smile1 smile1


If you replace echo str_replace($oldString, $newString, $data); then I get this result. (Generally wrong)

smile1smile2smile3


How to make such a result

smile1 smile2 smile3

PS Is there another option ?!

  • I do not see the problem. ideone.com/mUWTGH - ReinRaus
  • @ReinRaus Hello yes, I know, in fact, 😀 😃 😄 😀 😃 😄 these codes are so smiles πŸ˜€ πŸ˜ƒ πŸ˜„ - KYRAN
  • @ReinRaus for example, I will write ΠŸΡ€ΠΈΠ²Π΅Ρ‚ πŸ˜€ and this code is looking for smiles and replaced with the words ΠŸΡ€ΠΈΠ²Π΅Ρ‚ smile1 - KYRAN
  • In the input line that needs to be processed what is? unicode characters or html-entites of these characters? - ReinRaus
  • @ReinRaus here is the last mb_convert_encoding($data, 'UTF-8', 'HTML-ENTITIES'); - KYRAN

2 answers 2

So if they exactly match each other, why should you use replace? It may be worth creating an associative array of key-value type, for example:

 $array = []; $array['😀'] = 'smile1'; 

And that's it, then the value of 😀 comes in the $code variable for example you do $array[$code] and get smile1

  • Just write like that? works without return? Checked out ?! - KYRAN
  • function filter($data) { $array = []; $array['😀'] = 'smile1'; $array['😃'] = 'smile2'; $array['😄'] = 'smile3'; return $array[$data]; } function filter($data) { $array = []; $array['😀'] = 'smile1'; $array['😃'] = 'smile2'; $array['😄'] = 'smile3'; return $array[$data]; } does not work and also foreach ($hashtweet[1] as $ht){ $array = []; $array['😀'] = 'smile1'; $array['😃'] = 'smile2'; $array['😄'] = 'smile3'; $data = preg_replace("/(&#[a-zA-Z0-9]{6};)", $array[strtolower($ht)], $data); } foreach ($hashtweet[1] as $ht){ $array = []; $array['😀'] = 'smile1'; $array['😃'] = 'smile2'; $array['😄'] = 'smile3'; $data = preg_replace("/(&#[a-zA-Z0-9]{6};)", $array[strtolower($ht)], $data); } foreach ($hashtweet[1] as $ht){ $array = []; $array['😀'] = 'smile1'; $array['😃'] = 'smile2'; $array['😄'] = 'smile3'; $data = preg_replace("/(&#[a-zA-Z0-9]{6};)", $array[strtolower($ht)], $data); } does not work - KYRAN
  • replace() is necessary because you can replace only smiles. There is such a value echo smile('Hello! 😀 😃 😄'); and get Hello! smile1 smile2 smile3 Hello! smile1 smile2 smile3 - KYRAN

I found the answer myself using preg_replace_callback() . The problem was in foreach()

 function filter($matches) { $oldString = array('😀','😃','😄'); $newString = array('smile1','smile2','smile3'); return str_replace($oldString, $newString, $matches[1]); } function smile($data) { $data = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) { $char = current($m); $utf = iconv('UTF-8', 'UCS-4', $char); return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0")); }, $data); $data = preg_replace_callback('/(&#[a-zA-Z0-9]{6};)/', 'filter', $data); $data = mb_convert_encoding($data, 'UTF-8', 'HTML-ENTITIES'); return $data; } echo smile('😀 😃 😄'); 

smile1 smile2 smile3