Php preg_replace与元素中的数组

时间:2017-11-30 08:20:12

标签: php arrays preg-replace emoji

我正在尝试使用表情符号系统,如果数组中的元素通过页面,请转动另一侧的值。例如:

 $emoticons = array(
      ':)'    =>  '<img src="assets/smiles/smilesblank.png" alt="smile" class="img-responsive" />',
      ':-)'   =>  '<img src="assets/smiles/smilesblank.png" alt="smile" class="icon_smile" />',
      ':D'    =>  '<img src="assets/smiles/smilesblank.png" alt="smile" class="icon_laugh" />',
      ':d'    =>  '<img src="assets/smiles/smilesblank.png" alt="laugh" class="icon_laugh" />',
      ';)'    =>  '<img src="assets/smiles/smilesblank.png" alt="wink" class="icon_wink" />',
      ':P'    =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
      ':-P'   =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
      ':-p'   =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
      ':p'    =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
      ':('    =>  '<img src="assets/smiles/smilesblank.png" alt="sad face" class="icon_sad" />',
      ':o'    =>  '<img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shock" />',
      ':O'    =>  '<img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shock" />',
      ':0'    =>  '<img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shack" />',
      ':|'    =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />',
      ':-|'   =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />',
      ':/'    =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />',
      ':-/'   =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />'
 );
 foreach($emoticons as $icon => $image) {
      $icon = preg_quote($icon);
      $text = preg_replace("~\b$icon\b~",$image,$text);
 }

2 个答案:

答案 0 :(得分:1)

在大多数情况下,您不需要在匹配表情符号时检查单词边界,因为它们通常粘贴到其他文本上。因此,\b相当多余,可以删除。如果您确实只想定位特定的上下文,可以考虑将第一个(?<!\w)替换为(?!\w)(如果表情符号不能以字符char开头),第二个替换为(?<!\S)(没有单词char),或分别使用空格边界,(?!\S):-||

您可以按降序排列数组的键(如果有重叠的表情符号,如:-|preg_quote),则构建基于交替的正则表达式模式{{1}使用密钥,并使用preg_replace_callback将每个密钥替换为其对应的值:

array_multisort(array_map('strlen', array_keys($emoticons)), SORT_DESC, $emoticons);        //   IN DESCENDING ORDER
$pattern = '~' . implode("|", array_map(function($x) {return preg_quote($x, '~');}, array_keys($emoticons))) . '~';
echo preg_replace_callback($pattern, function($m) use ($emoticons) {
       return $emoticons[$m[0]];
    }, "Some :| emoticon :O");

请参阅PHP demo,了解结果为"Some :| emoticon :O"字符串

Some <img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" /> emoticon <img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shock" />

答案 1 :(得分:0)

您可以使用preg_replace_callback_array

它使用了一系列模式 - &gt;替换函数几乎与你的相同。

我为你做了一个小例子:

<?php

// initial array
$emoticons = [
    ':)'    =>  '<img src="assets/smiles/smilesblank.png" alt="smile" class="img-responsive" />',
    ':-)'   =>  '<img src="assets/smiles/smilesblank.png" alt="smile" class="icon_smile" />',
    ':D'    =>  '<img src="assets/smiles/smilesblank.png" alt="smile" class="icon_laugh" />',
    ':d'    =>  '<img src="assets/smiles/smilesblank.png" alt="laugh" class="icon_laugh" />',
    ';)'    =>  '<img src="assets/smiles/smilesblank.png" alt="wink" class="icon_wink" />',
    ':P'    =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
    ':-P'   =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
    ':-p'   =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
    ':p'    =>  '<img src="assets/smiles/smilesblank.png" alt="tounge" class="icon_tounge" />',
    ':('    =>  '<img src="assets/smiles/smilesblank.png" alt="sad face" class="icon_sad" />',
    ':o'    =>  '<img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shock" />',
    ':O'    =>  '<img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shock" />',
    ':0'    =>  '<img src="assets/smiles/smilesblank.png" alt="shock" class="icon_shack" />',
    ':|'    =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />',
    ':-|'   =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />',
    ':/'    =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />',
    ':-/'   =>  '<img src="assets/smiles/smilesblank.png" alt="straight face" class="icon_straight" />'
];

// prepare callbacks
$callbacks = [];
foreach ($emoticons as $smileCode => $replacement) {
    // regular expression, nothing smart, just plain replacement
    $regex = '~' . preg_quote($smileCode, '~') . '~';
    $callbacks[ $regex ] = function () use ($replacement) {
        return $replacement;
    };
}

$text = "Hello :), this is cool :P smile and :-| another one";

echo "<pre>\n";
echo preg_replace_callback_array($callbacks, $text);