-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" />',
                ':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

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

4

5 回答 5

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,包括转换后的表情。

于 2013-09-03T16:31:04.897 回答
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);

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

让你的 foreach 循环像这样:

$return = $text;
foreach($icons as $icon=>$image) {
       $icon = preg_quote($icon, '/');
       $return = preg_replace("/$icon/i", $image, $return);
}
于 2013-09-03T16:23:50.630 回答
2

的值中可能有一个斜线$icon,因此您实际上是在调用

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

这是一个错误。

于 2013-09-03T16:12:05.733 回答
2

preg_quote除非您告诉它这是您选择的分隔符,否则不会转义斜杠。

话虽这么说,使用 PREG 是大材小用。

改为使用str_ireplace

于 2013-09-03T16:12:37.957 回答
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
笑的表情是重复的。

于 2013-09-03T17:09:42.660 回答