Php 如何匹配以开头的字符串中的所有空格
我有一些文本和标签的HTML。比如说,Php 如何匹配以开头的字符串中的所有空格,php,regex,preg-replace,Php,Regex,Preg Replace,我有一些文本和标签的HTML。比如说, Outer text with [some random text title="My Title Here"], and more text 我想返回此html,但替换 1. “表示无任何内容 2.用于 从title=开始到] 因此,上述html的结果将是 Outer text with [some random text title=My Title Here], and more text. 在使用PHP时,我
Outer text with [some random text title="My Title Here"], and more text
我想返回此html,但替换1. <代码>“表示无任何内容
2.用于
从title=
开始到]
因此,上述html的结果将是
Outer text with [some random text title=My Title Here], and more text.
在使用PHP时,我想使用preg\u replace
,但不知道如何构造正确的搜索语句
我已经实现了类似这样的功能(^title=(.+)])
,但这只匹配title=“我的title Here”]
-我需要的是从中减去空格和双引号。您可以使用和来实现这一功能
echo preg_replace_callback('/(title=)([^\]]+)/', function($match) {
return $match[1] . str_replace(array('"', ' '), array('', ' '), $match[2]);
}, 'Outer text with [some random text title="My Title Here"], and more text');
正则表达式
(title=)([^\]]+)
捕获标题
和]
之间的所有内容。有关更详细的说明,请参阅:
演示:
它也可以在没有捕获组的情况下完成(假设起始锚点中没有双引号或空格)。您是否尝试自己写一些东西,或者是否正在找人为您做作业?使用str_replace(“,”,$string);@VijayaVigneshKumar-这将替换字符串中的所有空格,而问题是只替换一些空格。学习和测试正则表达式的最佳地方: