Php 使用带有引号内转义引号的正则表达式
我有一个PHPPhp 使用带有引号内转义引号的正则表达式,php,regex,preg-match-all,Php,Regex,Preg Match All,我有一个PHPpreg\u match\u all和REGEX问题 我有以下代码: <?php $string= 'attribute1="some_value" attribute2="<h1 class=\"title\">Blahhhh</h1>"'; preg_match_all('/(.*?)\s*=\s*(\'|"|&#?\w+;)(.*?)\2/s', trim($string), $matches); print_r($matches)
preg\u match\u all
和REGEX问题
我有以下代码:
<?php
$string= 'attribute1="some_value" attribute2="<h1 class=\"title\">Blahhhh</h1>"';
preg_match_all('/(.*?)\s*=\s*(\'|"|&#?\w+;)(.*?)\2/s', trim($string), $matches);
print_r($matches);
?>
您可以通过以下方法解决此问题:
这个正则表达式并不完美,因为它是作为分隔符存在的实体,就像引号一样,它也可以用\
转义。不知道这是否真的是有意的
另请参阅此伟大的问题/答案:。我们可以知道您在这段代码中到底想做什么吗?此问题甚至适用于\n个字符。谢谢你的回答,哈克雷!
Array
(
[0] => Array
(
[0] => attribute1="some_value"
[1] => attribute2="<h1 class=\"title\">Blahhhh</h1>"
)
[1] => Array
(
[0] => attribute1
[1] => attribute2
)
[2] => Array
(
[0] => "
[1] => "
)
[3] => Array
(
[0] => some_value
[1] => <h1 class=\"title\">Blahhhh</h1>
)
)
'/(.*?)\s*=\s*(\'|"|&#?\w+;)(.*?)(?<!\\\\)\2~/'
^^^^^^^^^
Array
(
[0] => Array
(
[0] => attribute1="some_value"
[1] => attribute2="<h1 class=\"title\">Blahhhh</h1>"
)
[1] => Array
(
[0] => attribute1
[1] => attribute2
)
[2] => Array
(
[0] => "
[1] => "
)
[3] => Array
(
[0] => some_value
[1] => <h1 class=\"title\">Blahhhh</h1>
)
)