单一的PHP函数替换表情符号或过滤词的文字 [英] Single PHP Function To Replace Text With Emoticons Or Filter Words
问题描述
$ p
$ b
$ smiles = array(
'xD' =>'devil.png',
'> :)'=> '('=>'cry.png',
':*'='devil.png',
'x('=>'angry.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'
);
$ b foreach($ smiles as $ key => $ img){
$ msg = str_replace($ key,'< img src =情绪/'.$ img。' height =18width =18/>',$ msg);
}
echo $ msg;
这似乎很简单,但是如果我想添加类似于的东西BadWord1'=> '********'
我知道如何将其添加到此脚本中,因为我只是添加了新行,但它会尝试把它变成一个图像。
是否可以写一个函数来替换文本和图像?
在很长一段时间里,我也想删除textarea换行符,并用< br>
替换它们,而不是像 $ val = str_replace(array(\ n,\r,\r\\\
),'< br />',$ val);
但是我似乎无法想象在一个函数内完成所有这三个操作的方法。
我的主要目标是提交textarea来调用文本到像 replaceText($ textareaText)
这样的函数,并且替换需要替换的文本内的任何内容。 b
$ b
我需要单独的函数吗?
我将继续自己的工作,所以如果我想出任何可能的我将更新我的发展问题包括它。
编辑:这是我想出了。你会认为这是足够的吗?
$ b $ pre $ 函数replaceText($味精){
$ replaceableText = array(
'xD'=>'< img src =emoticons / devil.pngheight =18width =18>',
'> :)'=> '< img src =emoticons / devil.pngheight =18width =18>',
'x('=>'< img src =emoticons / angry.png ((''=>'< img src =emoticons / cry.pngheight =18width =18 >',
':*'=>'< img src =emoticons / kiss.pngheight =18width =18>',
':)) '=>'< img src =emoticons / laugh.pngheight =18width =18>',
':D'=>'< img src = /laugh.pngheight =18width =18>',
':-D'=>'< img src =emoticons / laugh.pngheight =18width =18>',
':x'=>'< img src =emoticons / love.pngheight =18width =18>',
'(:|'=>'< img src =emoticons / sleepy.pngheight =18width =18>',
':)'=>'< img src =emoticons / smile.pngheight =18width =18>',
':-)'=> '< img src =emoticons / smile.pngheight =18width =18>',
':('=>'< img src =emoticons / sad.png height =18width =18>',
':-('=>'< img src =emoticons / sad.pngheight =18width =18 >',
';)'=>'< img src =emoticons / wink.pngheight =18width =18>',
';-) '=> '< img src =emoticons / wink.pngheight =18width =18>',
'\\\
'=> '< br>',
'\r'=> '< br>',
'\r\\\
'=> '< br>',
'\\\
\r'=> '< br>',
'badword1'=> '********',
'badword2'=> '********'
);
$ b foreach($ replaceableText as $ replace => $ replacedWith){
$ msg = str_replace($ replace,$ replacedWith,$ msg);
}
返回$味精;
$ b编辑2:
$ b 我忘了提到这一点,但这是一个HTML电子邮件脚本。
这样我可以键入一些简单的,如< h1>
,它会自动转换为带有预设内联样式的标头标签。
可能是这样的:
函数replaceText($ msg){
$ replaceableText = array(
'xD'=>'< img src = emoticons / devil.pngheight =18width =18>',
'> :)'=> '< img src =emoticons / devil.pngheight =18width =18>',
'x('=>'< img src =emoticons / angry.png ((''=>'< img src =emoticons / cry.pngheight =18width =18 >',
':*'=>'< img src =emoticons / kiss.pngheight =18width =18>',
':)) '=>'< img src =emoticons / laugh.pngheight =18width =18>',
':D'=>'< img src = /laugh.pngheight =18width =18>',
':-D'=>'< img src =emoticons / laugh.pngheight =18width =18>',
':x'=>'< img src =emoticons / love.pngheight =18width =18>',
'(:|'=>'< img src =emoticons / sleepy.pngheight =18width =18>',
':)'=>'< img src =emoticons / smile.pngheight =18width =18>',
':-)'=> '< img src =emoticons / smile.pngheight =18width =18>',
':('=>'< img src =emoticons / sad.png height =18width =18>',
':-('=>'< img src =emoticons / sad.pngheight =18width =18 >',
';)'=>'< img src =emoticons / wink.pngheight =18width =18>',
';-) '=> '< img src =emoticons / wink.pngheight =18width =18>',
'\\\
'=> '< br>',
'\r'=> '< br>',
'\r\\\
'=> '< br>',
'\\\
\r'=> '< br>',
'badword1'=> '********',
'badword2'=> '********',
'< h1>'=> '< h1 style =InlineStylesForHTMLEmail>'
);
foreach($ replaceableText as $ replace => $ replacedWith){
$ msg = str_replace($ replace,$ replacedWith,$ msg);
}
返回$味精;
如果这是我的项目,我会这样做。
$ array = [
'< img src =表情符号/ {{值}}height =18width =18>'=> [
':)'=> 'smile.png',
';)'=> 'wink.png'
],
'< br>'=> ['\ n','\r'],
'****'=> ['4lettercussword','4lettercussword'],
'*****'=> '5lettercussword'
];
$ b函数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); ($ i = 0; $ i< count($ value); $ i ++){
$ msg = str_replace($ value [$ i],$ key,$ msg);
} else {
$ msg = str_replace($ value,$ key,$ msg);
}
}
}
$ msg ='4lettercussword :) \\\
';
filterText($ array,$ msg);
echo $ msg;
输出:
****< img src =emoticons / smile.pngheight =18width =18> <峰; br>
数组中的键是替换值。如果密钥包含{{value}}标识符,则知道指向的数组将被关联,并且需要从该数组中获取值并将其插入到密钥中的{{value}}标识符中。如果任何键等于一个简单的数组值,它将用键替换任何这些值。这总是你有不同的HTML标签,并用一个键值str_replace替换它的一部分。
So I'm trying to find a way to build a function that will replace text with an image if it's an emoticon or with a filtered word if it is a prohibited word.
I've taken a look at this code:
$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;
That seems simple enough, but what if I want to add something like 'BadWord1' => '********'
I know how to add it to this script since I would just add that new line, but it would try to turn it into an image.
Is it possible to write a function that would replace both text and images?
On a long stretch I'm also wanting to remove textarea line breaks and replace them with <br>
instead using something like $val = str_replace( array("\n","\r","\r\n"), '<br />', $val );
But I can't seem to think of a way to accomplish all three within one function.
My main goal is when a textarea is submitted to call the text to the function like replaceText($textareaText)
and anything within the text that needs replaced gets replaced.
Will I need separate functions?
I am going to keep working on this on my own so if I come up with any possible developments I will update my question to include it.
EDIT: Here is what I have come up with. Would you consider this sufficient?
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;
}
EDIT 2:
I forgot to mention this earlier, but this is for an HTML email script.
This way I can type something simple such as <h1>
and it automatically gets converted to a header tag with preset inline styles.
Maybe something like this:
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;
}
[edit] sorry I couldn't help but do it the way I would if it were my project. A repeatable non-redundant process.
$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;
output:
**** <img src="emoticons/smile.png" height="18" width="18"> <br>
The key in the array is what will replace the value. If the key includes a {{value}} identifier then it knows the array pointed to will be associative, and that it needs to take the value from that array and plug it into the {{value}} identifier in your key. If any key equals a simple array of values it will replace any of those values with the key. This always you to have different html tags and replace only portions of it with a key value str_replace.
这篇关于单一的PHP函数替换表情符号或过滤词的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!