douhu4091 2018-08-06 09:50
浏览 23
已采纳

为什么str_ireplace会出现意外行为并仅针对一个针复制数据?

I have wrote a small piece of code for converting some food ingredients from the US format to international format.

To achieve this, I am using str_ireplace. This is working for everything except the needle "Blue 1". The expected output is Brilliant blue (E133) * but the actual output is Brilliant blue (E133) * (E133) *

Why is str_ireplace adding another (E133) * to the end of the string?

$ingredients = "Blue 1";

$find_colors = array(
    'Allura red',
    'Food Red 17',
    'C.I. 16035',
    'Red 40',
    'Carmoisine',
    'Azorubine',
    'Food Red 3',
    'Azorubin S',
    'Brilliantcarmoisin O',
    'Red 14',
    'C.I. 14720',
    'Ponceau 4R',
    'Cochineal Red A',
    'C.I. 16255',
    'Acid Red 18',
    'Brilliant Scarlet 3R',
    'Brilliant Scarlet 4R',
    'New Coccine',
    'SX Purple',
    'Quinoline yellow',
    'C.I. 47005',
    'Yellow 13',
    'Acid Yellow 3',
    'Sunset yellow',
    'Yellow 6',
    'C.I. 15985',
    'Tartrazine',
    'Yellow 5',
    'Blue 1',
    'Brilliant Blue',
    'Acid Blue 9',
    'Blue 2',
    'Yellow #5',
    'C.I. 19140',
    'red 3'
);
$replace_colors = array(
    'Allura red (E129) *',
    'Allura red (E129) *',
    'Allura red (E129) *',
    'Allura red (E129) *',
    'Carmoisine (E122) *',
    'Carmoisine (E122) *',
    'Carmoisine (E122) *',
    'Carmoisine (E122) *',
    'Carmoisine (E122) *',
    'Carmoisine (E122) *',
    'Carmoisine (E122) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Ponceau 4R (E124) *',
    'Quinoline yellow (E104) *',
    'Quinoline yellow (E104) *',
    'Quinoline yellow (E104) *',
    'Quinoline yellow (E104) *',
    'Sunset yellow (E110) *',
    'Sunset yellow (E110) *',
    'Sunset yellow (E110) *',
    'Tartrazine (E102) *',
    'Tartrazine (E102) *',
    'Brilliant blue (E133) *',
    'Brilliant blue (E133) *',
    'Brilliant blue (E133) *',
    'Indigo carmine (E132) *',
    'Tartrazine (E102) *',
    'Tartrazine (E102) *',
    'Allura red (E129) *'
);

$ingredients = str_ireplace($find_colors, $replace_colors, $ingredients);

echo $ingredients;

This behaviour does not seem to occur with any other needle. Additionally, if I remove all other needles and haystack, the issue does not occur.

Why is str_ireplace repeating these characters?

  • 写回答

1条回答 默认 最新

  • doule0941 2018-08-06 10:14
    关注

    It's because you also have Brilliant Blue as an option, which if finds once you replace blue 1 with Brilliant blue (E133) *.

    This simplest solution is to always look for the longest replacement string first, so switch the options...

    'Brilliant Blue',
    'Blue 1',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Linux环境下CA证书更新问题
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?