【问题标题】:Single PHP Function To Replace Text With Emoticons Or Filter Words用表情符号或过滤词替换文本的单个 PHP 函数
【发布时间】:2015-07-25 23:22:15
【问题描述】:

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

我看过这段代码:

$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' =&gt; '********' 的内容怎么办

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

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

在很长一段时间内,我还想删除 textarea 换行符并用 &lt;br&gt; 替换它们,而不是使用类似 $val = str_replace( array("\n","\r","\r\n"), '&lt;br /&gt;', $val ); 的东西

但我似乎想不出一种方法可以在一个功能中完成所有三个功能。

我的主要目标是当提交文本区域以将文本调用到 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 电子邮件脚本。

这样我可以输入一些简单的东西,例如&lt;h1&gt;,它会自动转换为带有预设内联样式的标题标签。

可能是这样的:

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;
}

【问题讨论】:

  • str_replace 接受数组。
  • 您的用户定义函数可以根据需要包含任意数量的 php 函数

标签: php foreach filter str-replace emoticons


【解决方案1】:

[edit] 抱歉,如果这是我的项目,我会按我的方式去做。一个可重复的非冗余过程。

$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 替换其中的一部分。

【讨论】:

  • 换行符呢?可以合并吗?我不确定您何时第一次查看我的问题,但我添加了对我编写的代码的编辑,我很好奇它是否能正确解决问题。
  • 我将其添加到编辑中。对于那个很抱歉。我认为这些永远不会改变,或者至少不会改变到足以将它们硬编码到函数中。
  • 哦,你不想那样做。给我一点时间。我错过了那里的重点。我会想办法的。
  • 我喜欢你的做法,我测试了我的编辑,但它似乎没有工作。任何想法我可能做错了什么?我以为我有,但是初学者错误:)
  • 秒。我意识到这里的主要区别是图像。其他所有内容都直接通过键值更改,因此我们要检查图像而不是相反。这样你的数组总是可以编辑的。而且您不希望将
【解决方案2】:

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

这是我的代码 sn-p。

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 2019-09-20
    • 2021-01-13
    • 1970-01-01
    • 2011-01-23
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多