douxuanma4357 2012-11-19 07:21
浏览 35
已采纳

PHP str_replace奇怪的结果

i have these codes:

$alphabet = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

$first = array("Captain","Dirty","Squidlips","Bowman","Buccaneer","Two Toes","Sharkbait","Old","Peg Leg","Fluffbucket","Scallywag","Bucko","Dead man","Matey","Jolly","Stinky","Bloody","Miss","Mad","Red","Lady","Bretheren","Rapscallion","Landlubber","Wench","Freebooter");

ImageTTFText($image, 45, 0, 0, $y-intval("30"), imageColorAllocate($image,255,255,255), "pirate-font.ttf", str_replace($alphabet,$first,"bad")); 

Please help me fix this weird problem... i think there is something wrong they i coded it, but i don't know which is which...

With the code above...

supposedly, the output must be Dirty Captain Bowman but it's odd that it outputs the wrong result...

check this: http://alylores.x10.mx/106/pic.php

Please help me fix my problem...

  • 写回答

2条回答 默认 最新

  • dstbtam8732 2012-11-19 07:36
    关注

    The left to right functionality of str_replace causes this, unfortunately. So here is an alternative.

    Here is an example of the code below: Example.

    $alphabet = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
    
    $first = array("Captain","Dirty","Squidlips","Bowman","Buccaneer","Two Toes","Sharkbait","Old","Peg Leg","Fluffbucket","Scallywag","Bucko","Dead man","Matey","Jolly","Stinky","Bloody","Miss","Mad","Red","Lady","Bretheren","Rapscallion","Landlubber","Wench","Freebooter");
    
    // split bad into an array, each letter being its own value.
    $input = str_split('bad');
    
    // Alphabet become the keys, $first are the values
    $c = array_combine($alphabet, $first);
    
    $output = '';
    foreach ($input as $letter)
    {
        $output .= $c[$letter] . ' ';
    }
    
    $final_word = trim($output);
    
    ImageTTFText($image, 45, 0, 0, $y-intval("30"), imageColorAllocate($image,255,255,255), "pirate-font.ttf", $final_word); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?