单个PHP函数用表情符号替换文本或过滤单词

时间:2015-07-25 23:22:15

标签: php foreach filter str-replace emoticons

所以我试图找到一种方法来构建一个函数,如果它是一个表情符号,则用图像替换文本,如果它是一个禁止的单词,则用过滤后的单词替换。

我已经看过这段代码:

$smiles = array(
  'xD'  => 'devil.png',
  '>:)' => 'devil.png',
  'x('  => 'angry.png',
  ':((' => 'cry.png',
  ':*'  => 'kiss.png',
  ':))' => 'laugh.png',
  ':D'  => 'laugh.png',
  ':-D' => 'laugh.png',
  ':x'  => 'love.png',
  '(:|' => 'sleepy.png',
  ':)'  => 'smile.png',
  ':-)' => 'smile.png',
  ':('  => 'sad.png',
  ':-(' => 'sad.png',
  ';)'  => 'wink.png',
  ';-)' => 'wink.png'
);

foreach($smiles as $key => $img) {
    $msg = str_replace($key, '<img src="emotions/'.$img.'" height="18" width="18" />', $msg);
}
echo $msg;

这似乎很简单,但如果我想添加'BadWord1' => '********'

之类的内容,该怎么办?

我知道如何将它添加到此脚本,因为我只是添加新行,但它会尝试将其转换为图像。

是否可以编写一个可以替换文本和图像的函数?

长期以来,我还希望删除textarea换行符并将其替换为<br>,而不是使用类似$val = str_replace( array("\n","\r","\r\n"), '<br />', $val );

的内容

但我似乎无法想出一种在一个功能中完成所有这三种功能的方法。

我的主要目标是提交textarea以将文本调用到函数replaceText($textareaText),并且需要替换的文本中的任何内容都会被替换。

我需要单独的功能吗?

我将继续自己动手,所以如果我想出任何可能的发展,我会更新我的问题以包含它。

编辑:这是我想出的。你认为这足够了吗?

function replaceText($msg) {
$replaceableText = array(
  'xD'  => '<img src="emoticons/devil.png" height="18" width="18">',
  '>:)' => '<img src="emoticons/devil.png" height="18" width="18">',
  'x('  => '<img src="emoticons/angry.png" height="18" width="18">',
  ':((' => '<img src="emoticons/cry.png" height="18" width="18">',
  ':*'  => '<img src="emoticons/kiss.png" height="18" width="18">',
  ':))' => '<img src="emoticons/laugh.png" height="18" width="18">',
  ':D'  => '<img src="emoticons/laugh.png" height="18" width="18">',
  ':-D' => '<img src="emoticons/laugh.png" height="18" width="18">',
  ':x'  => '<img src="emoticons/love.png" height="18" width="18">',
  '(:|' => '<img src="emoticons/sleepy.png" height="18" width="18">',
  ':)'  => '<img src="emoticons/smile.png" height="18" width="18">',
  ':-)' => '<img src="emoticons/smile.png" height="18" width="18">',
  ':('  => '<img src="emoticons/sad.png" height="18" width="18">',
  ':-(' => '<img src="emoticons/sad.png" height="18" width="18">',
  ';)'  => '<img src="emoticons/wink.png" height="18" width="18">',
  ';-)' => '<img src="emoticons/wink.png" height="18" width="18">',
  '\n' => '<br>',
  '\r' => '<br>',
  '\r\n' => '<br>',
  '\n\r' => '<br>',
  'badword1' => '********',
  'badword2' => '********'
);

foreach($replaceableText as $replace => $replacedWith) {
    $msg = str_replace($replace, $replacedWith, $msg);
}
return $msg;
}

编辑2:

我之前忘记提到这一点,但这是针对HTML电子邮件脚本的。

这样我可以输入一些简单的内容,例如<h1>,它会自动转换为带有预设内联样式的标题标记。

也许是这样的:

function replaceText($msg) {
$replaceableText = array(
  'xD'  => '<img src="emoticons/devil.png" height="18" width="18">',
  '>:)' => '<img src="emoticons/devil.png" height="18" width="18">',
  'x('  => '<img src="emoticons/angry.png" height="18" width="18">',
  ':((' => '<img src="emoticons/cry.png" height="18" width="18">',
  ':*'  => '<img src="emoticons/kiss.png" height="18" width="18">',
  ':))' => '<img src="emoticons/laugh.png" height="18" width="18">',
  ':D'  => '<img src="emoticons/laugh.png" height="18" width="18">',
  ':-D' => '<img src="emoticons/laugh.png" height="18" width="18">',
  ':x'  => '<img src="emoticons/love.png" height="18" width="18">',
  '(:|' => '<img src="emoticons/sleepy.png" height="18" width="18">',
  ':)'  => '<img src="emoticons/smile.png" height="18" width="18">',
  ':-)' => '<img src="emoticons/smile.png" height="18" width="18">',
  ':('  => '<img src="emoticons/sad.png" height="18" width="18">',
  ':-(' => '<img src="emoticons/sad.png" height="18" width="18">',
  ';)'  => '<img src="emoticons/wink.png" height="18" width="18">',
  ';-)' => '<img src="emoticons/wink.png" height="18" width="18">',
  '\n' => '<br>',
  '\r' => '<br>',
  '\r\n' => '<br>',
  '\n\r' => '<br>',
  'badword1' => '********',
  'badword2' => '********',
  '<h1>' => '<h1 style="InlineStylesForHTMLEmail">'
);
foreach($replaceableText as $replace => $replacedWith) {
    $msg = str_replace($replace, $replacedWith, $msg);
}
return $msg;
}

2 个答案:

答案 0 :(得分:1)

[编辑]抱歉,我忍不住按照我的方式去做,如果这是我的项目。可重复的非冗余过程。

$array = [
    '<img src="emoticons/{{value}}" height="18" width="18">' => [
        ':)' => 'smile.png', 
        ';)' => 'wink.png' 
    ],
    '<br>' => ['\n', '\r'],
    '****' => ['4lettercussword', '4lettercussword'],
    '*****' => '5lettercussword'
];

function filterText($array, &$msg) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
           if(array_keys($value) !== range(0, count($value) - 1)) {
              foreach($value as $k => $v) {
                  $msg = str_replace($k, str_replace('{{value}}', $v, $key), $msg);
              }
           } else {
               for($i = 0;$i < count($value);$i++) {
                   $msg = str_replace($value[$i], $key, $msg);
               }
           }
        } else {
            $msg = str_replace($value, $key, $msg);
        }
    }
}

$msg = '4lettercussword :) \n';
filterText($array, $msg);
echo $msg;

输出:

**** <img src="emoticons/smile.png" height="18" width="18"> <br>

数组中的键是替换值的内容。如果密钥包含{{value}}标识符,则它知道指向的数组将是关联的,并且它需要从该数组中获取值并将其插入密钥中的{{value}}标识符。如果任何键等于一个简单的值数组,它将用键替换任何这些值。这总是你有不同的html标签,只用键值str_replace替换它的一部分。

答案 1 :(得分:1)

nl2br将在字符串中的所有换行符之前插入HTML换行符。

这是我的代码段。

function replaceText($val)
{
    $search = array(
      'xD',
      '>:)',
      'x(',
      ':((',
      ':*',
      ':))',
      ':D',
      ':-D',
      ':x',
      '(:|',
      ':)',
      ':-)',
      ':(',
      ':-(',
      ';)',
      ';-)',
      'Badword1'
    );

    $replace = array(
      '<img src="emotions/devil.png" height="18" width="18" />',
      '<img src="emotions/devil.png" height="18" width="18" />',
      '<img src="emotions/angry.png" height="18" width="18" />',
      '<img src="emotions/cry.png" height="18" width="18" />',
      '<img src="emotions/kiss.png" height="18" width="18" />',
      '<img src="emotions/laugh.png" height="18" width="18" />',
      '<img src="emotions/laugh.png" height="18" width="18" />',
      '<img src="emotions/laugh.png" height="18" width="18" />',
      '<img src="emotions/love.png" height="18" width="18" />',
      '<img src="emotions/sleepy.png" height="18" width="18" />',
      '<img src="emotions/smile.png" height="18" width="18" />',
      '<img src="emotions/smile.png" height="18" width="18" />',
      '<img src="emotions/sad.png" height="18" width="18" />',
      '<img src="emotions/sad.png" height="18" width="18" />',
      '<img src="emotions/wink.png" height="18" width="18" />',
      '<img src="emotions/wink.png" height="18" width="18" />',
      '********'
    );
    $val = str_replace( $search, $replace, $val );
    $val = nl2br($val);
    return $val;
}

replaceText($textareaText);