Single PHP Function To Replace Text With Emoticons Or Filter Words

左心房为你撑大大i 提交于 2020-01-04 14:02:08

问题


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

回答1:


[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.




回答2:


nl2br will insert HTML line breaks before all newlines in a string.

Here is my code snippet.

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


来源:https://stackoverflow.com/questions/31632059/single-php-function-to-replace-text-with-emoticons-or-filter-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!