Unexpected behavior from str_replace()

前端 未结 2 1463
清歌不尽
清歌不尽 2021-01-15 01:28

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 doi

相关标签:
2条回答
  • 2021-01-15 01:44

    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));
    
    0 讨论(0)
  • 2021-01-15 02:02

    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');
    
    0 讨论(0)
提交回复
热议问题