非常简单的preg_replace无法正常工作

时间:2013-09-03 16:09:29

标签: php preg-replace

function emotify($text)
{
    $icons = array(
                ':)'    =>  '<img src="/images/emoticons/happy.png" alt="smile" class="icon_smile" />',
                ':-)'   =>  '<img src="/images/emoticons/happy.png" alt="smile" class="icon_smile" />',
                ':D'    =>  '<img src="/images/emoticons/grin.png" alt="smile" class="icon_laugh" />',
                ':d'    =>  '<img src="/images/emoticons/grin.png" alt="laugh" class="icon_laugh" />',
                ":'("    =>  '<img src="/images/emoticons/crying.png" alt="crying" class="icon_cry" />',
                ';('    =>  '<img src="/images/emoticons/crying.png" alt="crying" class="icon_cry" />',
                ':d'    =>  '<img src="/images/emoticons/grin.png" alt="laugh" class="icon_laugh" />',
                ';)'    =>  '<img src="/images/emoticons/wink.png" alt="wink" class="icon_wink" />',
                ':P'    =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
                ':-P'   =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
                ':-p'   =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
                ':p'    =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
                ':('    =>  '<img src="/images/emoticons/sad.png" alt="sad face" class="icon_sad" />',
                ':-('   =>  '<img src="/images/emoticons/sad.png" alt="sad face" class="icon_sad" />',
                ':o'    =>  '<img src="/images/emoticons/shocked.png" alt="shock" class="icon_shock" />',
                ':O'    =>  '<img src="/images/emoticons/shocked.png" alt="shock" class="icon_shock" />',
                ':0'    =>  '<img src="/images/emoticons/shocked.png" alt="shock" class="icon_shack" />',
                ':|'    =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />',
                ':-|'   =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />',
                ':/'    =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />',
                ':-/'   =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />'
        );

     foreach($icons as $icon=>$image) {
      $icon = preg_quote($icon, '/');
$return = preg_replace("/$icon/i", $image, $text);
      }
    return $return;
}
$posted = emotify($posted);

这是我将表情符号转换为图像的代码。它只是返回
Warning: preg_replace(): Unknown modifier '/' on line 203

我该如何解决? 编辑:这是我的所有代码,包括数组,这可能是我的代码不工作的问题。我不确定,但没有答案有帮助,所以我认为问题一定是。

5 个答案:

答案 0 :(得分:4)

通过评论进一步确定了你正在做的事情,这是一个使用DOM解析器和一点聪明的答案:

$dom = new DOMDocument();
$dom->loadHTML($text);
$xpath = new DOMXPath($dom);
foreach($icons as $icon=>$image) {
  $textnodes = $xpath->query("//text()"); // get all text nodes
  foreach($textnodes as $node) {
    // the regex used here requires that there not be a non-space before it
    // in other words, it must be the first thing, or have a space before it
    while( preg_match("((?<!\S)".preg_quote($icon).")i",$node->nodeValue,$m,PREG_OFFSET_CAPTURE)) {
      $emote = $node->splitText($m[0][1]); // the offset is stored here
      $node = $emote->splitText(strlen($icon));
      // now replace the emote with the image:
      $img = $dom->createElement("img");
      $img->setAttribute("src",$image);
      // IMPORTANT: $image must be just the SRC, and NOT the entire HTML node
      $emote->parentNode->replaceChild($img,$emote);
    }
  }
}
// loadHTML puts the HTML into a document, we need to get it back out
$result = $dom->saveHTML($dom->getElementsByTagName('body')->item(0));
$result = substr($result,strlen("<body>"),-strlen("</body>"));

现在你可以使用$result,一切都很好,应该有有效的HTML,包括转换的表情。

答案 1 :(得分:2)

$icon的值可能有斜线,因此您实际上正在调用

preg_replace( '/foo/bar/i', $image, $text )

这是一个错误。

答案 2 :(得分:2)

preg_quote除非你告诉它这是你选择的分隔符,否则不会逃避斜杠。

话虽如此,使用PREG是过度杀伤。

改为使用str_ireplace

答案 3 :(得分:2)

preg_quote实际上有2个参数。第二个可选参数可以是regex delimiter中使用的preg functions

按照手册:

<强> delimiter

  

If the optional delimiter is specified, it will also be escaped. This is useful for escaping the delimiter that is required by the PCRE functions. The / is the most commonly used delimiter.

使用这样的代码:

$icon = preg_quote($icon, '/');
$return = preg_replace("/$icon/i", $image, $text);

更新:问题是您在开始foreach循环之前没有初始化$return变量,并且总是尝试替换原始的$text变量本身。

让你的foreach循环如下:

$return = $text;
foreach($icons as $icon=>$image) {
       $icon = preg_quote($icon, '/');
       $return = preg_replace("/$icon/i", $image, $return);
}

答案 4 :(得分:1)

你可以这样做:

function emotify($text)
{
    $icons = array(
        ':)'    =>  '<img src="/images/emoticons/happy.png" alt="smile" class="icon_smile" />',
        ':-)'   =>  '<img src="/images/emoticons/happy.png" alt="smile" class="icon_smile" />',
        ':D'    =>  '<img src="/images/emoticons/grin.png" alt="smile" class="icon_laugh" />',
        ':d'    =>  '<img src="/images/emoticons/grin.png" alt="laugh" class="icon_laugh" />',
        ':\'('  =>  '<img src="/images/emoticons/crying.png" alt="crying" class="icon_cry" />',
        ';('    =>  '<img src="/images/emoticons/crying.png" alt="crying" class="icon_cry" />',
        ';)'    =>  '<img src="/images/emoticons/wink.png" alt="wink" class="icon_wink" />',
        ':P'    =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
        ':-P'   =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
        ':-p'   =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
        ':p'    =>  '<img src="/images/emoticons/tounge.png" alt="tounge" class="icon_tounge" />',
        ':('    =>  '<img src="/images/emoticons/sad.png" alt="sad face" class="icon_sad" />',
        ':-('   =>  '<img src="/images/emoticons/sad.png" alt="sad face" class="icon_sad" />',
        ':o'    =>  '<img src="/images/emoticons/shocked.png" alt="shock" class="icon_shock" />',
        ':O'    =>  '<img src="/images/emoticons/shocked.png" alt="shock" class="icon_shock" />',
        ':0'    =>  '<img src="/images/emoticons/shocked.png" alt="shock" class="icon_shack" />',
        ':|'    =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />',
        ':-|'   =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />',
        ':/'    =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />',
        ':-/'   =>  '<img src="/images/emoticons/straight.png" alt="straight face" class="icon_straight" />'
        );
    return str_replace(array_keys($icons), array_values($icons), $text);
}

请注意,在这种情况下,最好定义两个数组。

注意:此类搜索/替换不得区分大小写:D :d
一个笑的表情符号是重复的。