Unexpected behavior from str_replace()

非 Y 不嫁゛ 提交于 2019-12-01 08:17:05

问题


I have two arrays. One with color names and the other with the RGB values.

I am converting a color name to it's RGB value using str_replace() (then doing some other stuff with it).

All of the colours work as expected, except Pale Yellow.

$colour = "Pale Yellow";
$RGBint     = array('Red'      ,'Burgundy','Rust'     ,'Electric Orange','Pumpkin'    ,'Melon'     ,'Baby Pink'  ,'Candy Floss Pink','Electric Pink','Yellow'    ,'Pale Yellow','Golden'    ,'Lime'     ,'Kiwi'       ,'Mint'       ,'Dragonfly Green','Kelly Green','Fern'       ,'Forest Green','Olive'     ,'Teal'      ,'Baby Blue'  ,'Dragonfly Blue','Cornflower' ,'Medium Blue','Royal Blue','Electric Blue','Navy'    ,'Lavender'   ,'Lilac'     ,'Purple'  ,'Plum'      ,'Dark Brown','Chocolate Brown','Light Brown','Copper'   ,'Beige'      ,'Linen'      ,'Taupe'      ,'Shimmer'    ,'Silver'     ,'Medium Grey','Charcoal'   ,'Black', 'White'     , 'Off White' , 'Neon Light Orange','Neon Orange','Neon Light Pink','Neon Dark Pink','Neon Yellow','Neon Green');
$ColourName = array("200,16,46","166,9,61","150,56,33","255,106,19"     ,"255,141,109","255,181,73","245,222,218","245,155,187"     ,"239,74,129"   ,"253,218,36","250,224,83" ,"203,160,82","206,220,0","142,221,101","128,224,167","169,196,127"    ,"0,132,61"   ,"142,221,101","0,87,63"     ,"103,130,58","39,153,137","171,202,233","189,214,230"   ,"123,175,212","95,143,180" ,"0,51,160"  ,"0,125,186"    ,"20,27,77","149,149,210","144,99,205","51,0,114","140,71,153","99,81,61"  ,"105,63,35"      ,"134,109,75" ,"115,56,29","219,200,182","176,170,126","138,126,112","208,211,212","162,172,171","142,144,137","112,115,114","0,0,0","255,255,255","227,223,195","255,170,77"        ,"255,143,108","255,95,162"     ,"239,66,111"    ,"224,231,33" ,"255,233,0");
$RGBvalue = str_replace($RGBint, $ColourName, $colour);
die($RGBvalue);

Expected result:

250,224,83

Actual result:

Pale 250,224,83

I don't understand why it is picking up "Pale" in the result, this isn't the only two word color, and the others work fine.

I can't see anything obvious as to why this is happening, What am I missing?


回答1:


That's because str_replace() finds yellow first (Because it's before Pale Yellow in the array) and after this it can't find Pale anymore. So use strtr() instead, like this:

$RGBvalue = strtr($colour, array_combine($RGBint, $ColourName));



回答2:


To fix the issue change the order of Yellow / Pale Yellow in the $RGBint array.

Correct:

$RGBint     = array('Red'      ,'Burgundy','Rust'     ,'Electric Orange','Pumpkin'    ,'Melon'     ,'Baby Pink'  ,'Candy Floss Pink','Electric Pink', 'Pale Yellow','Yellow'    ,'Golden'    ,'Lime'     ,'Kiwi'       ,'Mint'       ,'Dragonfly Green','Kelly Green','Fern'       ,'Forest Green','Olive'     ,'Teal'      ,'Baby Blue'  ,'Dragonfly Blue','Cornflower' ,'Medium Blue','Royal Blue','Electric Blue','Navy'    ,'Lavender'   ,'Lilac'     ,'Purple'  ,'Plum'      ,'Dark Brown','Chocolate Brown','Light Brown','Copper'   ,'Beige'      ,'Linen'      ,'Taupe'      ,'Shimmer'    ,'Silver'     ,'Medium Grey','Charcoal'   ,'Black', 'White'     , 'Off White' , 'Neon Light Orange','Neon Orange','Neon Light Pink','Neon Dark Pink','Neon Yellow','Neon Green');


来源:https://stackoverflow.com/questions/29891639/unexpected-behavior-from-str-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!