因此,我正在尝试找到一种方法来构建一个函数,如果文本是表情符号,则将其替换为图像;如果文本是禁用词,则将其替换为过滤后的词。
我看了一下这段代码:
$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' => '********'
的内容怎么办?
我知道如何将其添加到此脚本中,因为我只需添加该新行,但它会尝试将其转换为图像。
是否可以编写一个同时替换文本和图像的函数?
从长远来看,我还想删除文本区域换行符并将其替换为 <br>
而是使用类似 $val = str_replace( array("\n","\r","\r\n"), '<br />', $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 电子邮件脚本的。
这样我就可以输入一些简单的内容,例如 <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;
}
最佳答案
[编辑]抱歉,如果这是我的项目,我忍不住要按照我的方式去做。可重复的非冗余过程。
$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 仅替换其中的一部分。
关于php - 用表情符号或过滤词替换文本的单个 PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31632059/