2012-11-12 152 views
1

我正在开发一个脚本,它需要一篇文章,搜索文章中的“关键字”,然后用锚链接随机替换该关键字。用随机替换替换字符串的随机字

我有脚本工作,因为它应该,但是我需要能够有一个“替代”的数组循环和插入随机位置。

因此,第一个随机位置将获得锚链接#1。 第二个随机位置将获得锚链接#2。 第三个随机位置将获得锚链接#3。 等等

我找到了答案的一半我的问题在这里:PHP replace a random word of a string

public function replace_random ($str, $search, $replace, $n) { 

    // Get all occurences of $search and their offsets within the string 
    $count = preg_match_all('/\b'.preg_quote($search, '/').'\b/', $str, $matches, PREG_OFFSET_CAPTURE); 

    // Get string length information so we can account for replacement strings that are of a different length to the search string 
    $searchLen = strlen($search); 
    $diff = strlen($replace) - $searchLen; 
    $offset = 0; 

    // Loop $n random matches and replace them, if $n < 1 || $n > $count, replace all matches 
    $toReplace = ($n < 1 || $n > $count) ? array_keys($matches[0]) : (array) array_rand($matches[0], $n); 
    foreach ($toReplace as $match) { 
     $str = substr($str, 0, $matches[0][$match][1] + $offset).$replace.substr($str, $matches[0][$match][1] + $searchLen + $offset); 
     $offset += $diff; 
    } 

    return $str; 

} 

所以我的问题是,我怎样才能改变这种功能以接受$数组替换变量?

编辑 我也试图从mt_rand,从替代品的阵列输出我更换一些随机的数组键,但它仍然是只增加整个内容替换之一。我希望函数为它找到的$ search变量的每个实例选择一个“关键字”。

public function replace_random ($str, $search, $replace, $n) { 

    // Get all occurences of $search and their offsets within the string 
    $count = preg_match_all('/\b'.preg_quote($search, '/').'\b/', $str, $matches, PREG_OFFSET_CAPTURE); 

    // Get string length information so we can account for replacement strings that are of a different length to the search string 
    $searchLen = strlen($search); 
    $diff = strlen($replace) - $searchLen; 
    $offset = 0; 

    $searchCount = count($replace); 
    $arrayNum = mt_rand(0, 4); 

    // Loop $n random matches and replace them, if $n < 1 || $n > $count, replace all matches 
    $toReplace = ($n < 1 || $n > $count) ? array_keys($matches[0]) : (array) array_rand($matches[0], $n); 
    foreach ($toReplace as $match) { 
     $str = substr($str, 0, $matches[0][$match][1] + $offset).$replace[$arrayNum].substr($str, $matches[0][$match][1] + $searchLen + $offset); 
     $offset += $diff; 
    } 

    return $str; 

} 
+0

它是基本的algorythm,而不是程序问题...你只尝试一些东西吗? –

+0

是的,我试图使用一个数组的功能,它并没有替代......在一个侧面说明,我不明白这是如何不明确或无用的。我一直在研究这一个星期!这就是为什么我问社区...为什么负面代表?编辑 – tpickett

+0

后显示尝试解决问题,希望这使得它成为一个编程问题? – tpickett

回答

0

的解决方案横空出世,我需要一个变量添加到foreach循环:

$replaceRand = $replace[array_rand($replace)]; 

,并调用它在$ str变量:

$str = substr($str, 0, $matches[0][$match][1] + $offset).$replaceRand.substr($str, $matches[0][$match][1] + $searchLen + $offset); 

整个代码结束up:

public function replace_random ($str, $search, $replace, $n) { 

    // Get all occurences of $search and their offsets within the string 
    $count = preg_match_all('/\b'.preg_quote($search, '/').'\b/', $str, $matches, PREG_OFFSET_CAPTURE); 

    // Get string length information so we can account for replacement strings that are of a different length to the search string 
    $searchLen = strlen($search); 
    $diff = strlen($replace) - $searchLen; 
    $offset = 0; 

    // Loop $n random matches and replace them, if $n < 1 || $n > $count, replace all matches 
    $toReplace = ($n < 1 || $n > $count) ? array_keys($matches[0]) : (array) array_rand($matches[0], $n); 
    foreach ($toReplace as $match) { 
     $replaceRand = $replace[array_rand($replace)]; 
     $str = substr($str, 0, $matches[0][$match][1] + $offset).$replaceRand.substr($str, $matches[0][$match][1] + $searchLen + $offset); 
     $offset += $diff; 
    } 

    return $str; 

} 
相关问题