在php中编辑类似whatsapp的文本

时间:2018-12-22 11:50:30

标签: php regex

我正在尝试撰写whatsapp风格的文字文章。当用户创建这样的文本时:

function makeBoldText($orimessage){
    $message = $orimessage;
    $regex = "/\*([\w]*)\*/";
    $message = preg_replace($regex, '<strong>$0</strong>', $message);
    return  $message ;
}
echo makeBoldText($message);

然后此文本会像这样自动更改

,你好吗?你在哪里

我知道我可以使用php正则表达式来做到这一点:

该示例适用于粗体文本:

*

但是有一个问题,在输出文本时应将其/\*([\w]*)\*/ 删除。

另一个正则表达式也应该像这样:

粗体:

/_([\w]*)_/ 

斜体:

 /~([\w]*)~/

删除线:

server {
    listen  00.00.000.000:80;

    server_name my_site.com www.my_site.com;

    return 301 https://www.my_site.com$request_uri;
}

server {
    listen  00.00.000.000:443;

    server_name my_site.com www.my_site.com;

    error_log  /var/log/httpd/domains/my_site.com.error.log error;

    ssl on;
    ssl_certificate   cert/214816718480217.pem;
    ssl_certificate_key  cert/214816718480217.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass      http://00.00.000.000:8080;
        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|tif|tiff|css|js|htm|html|ttf|otf|webp|woff|txt|csv|rtf|doc|docx|xls|xlsx|ppt|pptx|odf|odp|ods|odt|pdf|psd|ai|eot|eps|ps|zip|tar|tgz|gz|rar|bz2|7z|aac|m4a|mp3|mp4|ogg|wav|wma|3gp|avi|flv|m4v|mkv|mov|mpeg|mpg|wmv|exe|iso|dmg|swf)$ {
            root           /home/my_site/web/my_site.com/public_html;
            access_log     /var/log/httpd/domains/my_site.com.log combined;
            access_log     /var/log/httpd/domains/my_site.com.bytes bytes;
            expires        max;
            try_files      $uri @fallback;
        }
    }

    location /error/ {
        alias   /home/my_site/web/my_site.com/document_errors/;
    }

    location @fallback {
        proxy_pass      http://00.00.000.000:8080;
    }

}

我的问题是,我可以在一个正则表达式中完成所有这些操作吗?并在输出时可以删除特殊字符吗?

4 个答案:

答案 0 :(得分:2)

我不知道是否可以在一个正则表达式中完成所有操作,但是对于第二个问题:“在输出时是否可以删除特殊字符” ,您可以像这样的东西:

$subject = "*Hi* ~how are you~ _where are you?_"
$message = preg_replace('/(?:\*)([^*]*)(?:\*)/', '<strong>$1</strong>', $subject);
$message = preg_replace('/(?:_)([^_]*)(?:_)/', '<i>$1</i>', $message);
$message = preg_replace('/(?:~)([^~]*)(?:~)/', '<strike>$1</strike>', $message);

答案 1 :(得分:2)

您可以在此处使用一次致电preg_replace_callback

$styles = array ( '*' => 'strong', '_' => 'i', '~' => 'strike');

function makeBoldText($orimessage) {
   global $styles;
   return preg_replace_callback('/(?<!\w)([*~_])(.+?)\1(?!\w)/',
      function($m) use($styles) { 
         return '<'. $styles[$m[1]]. '>'. $m[2]. '</'. $styles[$m[1]]. '>';
      },
      $orimessage);
}

// call it as:
$s = '*Hi* ~how are you~ _where are you?_';
echo makeBoldText($s);
//=> <strong>Hi</strong> <strike>how are you</strike> <i>where are you?</i>

答案 2 :(得分:0)

您可以使用数组performing multiple preg_replace with different search & replace each time

进行多个preg_replace。
$string = 'I have a match1 and a match3, and here\'s a match2';
$find = array('/match1/', '/match2/');
$replace = array('foo', 'bar');
$result = preg_replace($find, $replace, $string);

您可以使用pattern或str_replace之后删除*

preg_replacehttp://php.net/manual/en/function.preg-replace.php

str_replacehttp://php.net/manual/en/function.str-replace.php

答案 3 :(得分:0)

您可以在此处使用 javascript 的一个 UDF 函数在 javascript 上执行相同的操作

使用此函数使用正则表达式 (regex) 包装文本。

function wrap(str) {
  if (str) {
    return str
      .replace(/(?:\*)([^*]*)(?:\*)/gm, "<strong>$1</strong>")
      .replace(/(?:_)([^_]*)(?:_)/gm, "<i>$1</i>")
      .replace(/(?:~)([^~]*)(?:~)/gm, "<strike>$1</strike>")
      .replace(/(?:```)([^```]*)(?:```)/gm, "<tt>$1</tt>");
  } else {
    return str;
  }
}
let str="Hello *Hiren Raiyani*, This is testing text which has _italic_ text and ~Strick Text~ also, and ``` monospace text also exists ```"; 

let htmlStr = wrap(str);
console.log(htmlStr);

document.getElementById("str-wrp").innerHTML = htmlStr;
<div id="str-wrp"></div>

示例:

let str="Hello *Hiren Raiyani*, This is testing text which has _italic_ text and ~Strick Text~ also, and ``` monospace text also exists ```;

wrap(str);

输出:

Hello <strong>Hiren</strong>, This is testing text which has <i>italic</i> text and <strick>Strick Text</strick> also, and <tt> monospace text also exists <tt>