我需要帮助。请假设以下PHP变量
$string="Hello this
is
me on [the
line
to
] have no clue";现在,我想将括号内的换行符替换为空格,以获得以下内容
Hello this
is
me on [the line to] have no clue有什么想法吗?我知道如何处理所有的换行符,但我不知道如何只处理括号内的换行符。
谢谢
发布于 2016-03-29 21:16:05
发布于 2016-03-29 21:22:43
您可以使用preg_replace_callback匹配[...]子字符串,并仅替换匹配中的换行符:
$s = "Hello this\nis\nme on [the\nline\nto\n] have no clue";
echo preg_replace_callback('/\[\s*([^][]*?)\s*]/', function($m){
return "[" . str_replace("\n", " ", $m[1]) . "]";
}, $s);请参阅IDEONE demo
[\[\s*([^][]*?)\s*]](https://regex101.com/r/bM0mB5/1)正则表达式说明:
\[ -方块开始bracket\s* - whitespace([^][]*?) - ]\s*和whitespace]之外的尽可能少的0+字符- 0+ [ - 0+ 发布于 2016-03-29 21:16:16
https://stackoverflow.com/questions/36285392
复制相似问题