0

我有这些代码:

$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")); 

请帮我解决这个奇怪的问题...我认为他们编码有问题,但我不知道哪个是哪个...

使用上面的代码...

据说,输出必须是Dirty Captain Bowman ,但奇怪的是它输出了错误的结果......

检查这个:http ://alylores.x10.mx/106/pic.php

请帮我解决我的问题...

4

2 回答 2

3

str_replace不幸的是,从左到右的功能会导致这种情况。所以这里有一个替代方案。

这是下面代码的示例:示例。

$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); 
于 2012-11-19T07:36:20.900 回答
2
<?php
// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);

// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search  = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit   = array('apple', 'pear');
$text    = 'a p';
$output  = str_replace($letters, $fruit, $text);
echo $output;
?>

来源: http: //php.net/manual/en/function.str-replace.php

伪代码:

  1. 将字符串拆分为数组(http://php.net/manual/en/function.str-split.php 拆分长度 = 1)
  2. 替换每个数组条目中的每个值
  3. 再次将绳子放在一起。
于 2012-11-19T07:27:38.613 回答