简体   繁体   English

正则表达式:删除所有多余的空格,换行符和空白

[英]Regex : Removing all extra whitespace, line-breaks, and empty spaces

Update: It seems like it is just a regex problem. 更新:看来这只是一个正则表达式问题。

I am trying to remove all extra whitespace, line-breaks, and empty spaces from user story with a function to grab only 100 characters 我正在尝试使用仅捕获100个字符的功能从用户故事中删除所有多余的空格,换行符和空白

Issue is although 100 character limit works, the removing of whitespace, linebreaks and empty spaces does not apply: 问题是,尽管可以使用100个字符的限制,但是空格,换行符和空格的删除不适用于:

function aboutme_echo($x, $length)
{ 
    if(strlen($x) <= $length) 
    { 
        echo $x; 
    }
    else
    {
        $y = substr($x,0,$length) . '...'; 
        echo $y; 
    } 
}

aboutme_echo((preg_replace("/\s+/"," ", $aboutme)), 100);

Example String: 🤣🤣🤣WHAT?! 示例字符串:🤣🤣🤣WHAT?! That's crazy! 太疯狂了!

Long story short, 长话短说,

😛😛someone reached out to me who had a pharma virus. 😛😛有人联系我,发现有人感染了药草病毒。 😎I have the opportunity to rebuild their site, but I can't rush the planning and staging, but i... 😎我有机会重建他们的网站,但是我不能匆忙进行计划和分期,但是我...

Something like this maybe? 像这样的东西?

function cleanExcerpt($string) {
   $pattern = '/\s+/';
   $replace = ' ';
   $cleanstring = trim(preg_replace($pattern,$replace,$string));
   return strtok(wordwrap($cleanstring, 100, "...\n"), "\n");
}

To use, you must pass through a string and echo the function like this: 要使用它,您必须传递一个字符串并回显如下函数:

$string = "Example String: 🤣🤣🤣WHAT?! That's crazy! Long story short, 😛😛someone reached ";

echo cleanExcerpt($string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM