简体   繁体   English

删除字符串末尾带有[]并在下一行开头[[]的字符串中的空格和回车符

[英]Remove empty space and carriage return from a string with ] at end and [ at the start of the next line

I am relatively new to regular expressions. 我对正则表达式比较陌生。 I just wanted to know that how to replace empty spaces or carriage return ( new line ) through preg_replace in php for this string: 我只是想知道如何通过php中的preg_replace替换此字符串的空白或回车符(换行):

[someshortcode]

[someshortcode]

The string has ] at the end of one line and [ at the new line. 字符串在一行的末尾有] ,在新行中有[ I want to remove any spaces/ characters be it '\\r' , '\\t' , '\\n' , empty spaces or any other character 我想删除任何空格/字符,无论是'\\r''\\t''\\n' ,空格还是任何其他字符

output should be like this: [someshortcode][someshortcode] 输出应该是这样的: [someshortcode][someshortcode]

Thanks 谢谢

$result = preg_replace("/\s/", '', $string);

You can learn more about the "\\s" and others in Escape sequences 您可以在转义序列中了解有关“ \\ s”和其他字符的更多信息

Update : 更新

I'd like to add an alternative with POSIX compliant regular expression, where you could use the following line, which by the way would also match the VT character (code 11): 我想添加一个兼容POSIX的正则表达式,您可以在其中使用以下行,顺便说一句,该行也将与VT字符(代码11)匹配:

$result = preg_replace("/[[:space:]]/", '', $string);

More about Character classes 有关角色类的更多信息

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

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