Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将html值和普通字符串拆分为不同的数组?_Php_Regex - Fatal编程技术网

如何在php中将html值和普通字符串拆分为不同的数组?

如何在php中将html值和普通字符串拆分为不同的数组?,php,regex,Php,Regex,假设我有如下字符串: "b<a=2<sup>2</sup>" “更新: 这似乎适用于以下公式: "(c<a) = (b<a) = 2<sup>2</sup>" "b<a=2<sup>2</sup>" ”(日期: 这似乎适用于以下公式: "(c<a) = (b<a) = 2<sup>2</sup>" "b<a=2<sup>2</sup&

假设我有如下字符串:

"b<a=2<sup>2</sup>"
“更新:
这似乎适用于以下公式:

"(c<a) = (b<a) = 2<sup>2</sup>"
"b<a=2<sup>2</sup>"
”(日期:
这似乎适用于以下公式:

"(c<a) = (b<a) = 2<sup>2</sup>"
"b<a=2<sup>2</sup>"

“(c可能您可以给空格,例如:

b < a = 2<sup>2</sup>
b

它不会消失标记,看起来更容易理解……

您可能可以给空格,例如:

b < a = 2<sup>2</sup>
b

它不会消失标记,看起来更容易理解……

您可以尝试这种正则表达式方法,它应该跳过元素

$regex = '/<(.*?)\h*.*>.+<\/\1>(*SKIP)(*FAIL)|(<|>)/';
$string = 'b<a=2<sup>2</sup>';
$string = preg_replace_callback($regex, function($match) {
    return htmlentities($match[2]);
}, $string);
echo $string;
$regex='/.+(*跳过)(*失败)|()/';
$string='b

Regex101:

您可以尝试这种regex方法,它应该跳过元素

$regex = '/<(.*?)\h*.*>.+<\/\1>(*SKIP)(*FAIL)|(<|>)/';
$string = 'b<a=2<sup>2</sup>';
$string = preg_replace_callback($regex, function($match) {
    return htmlentities($match[2]);
}, $string);
echo $string;
$regex='/.+(*跳过)(*失败)|()/';
$string='b

Regex101:

使用(或者可能
htmlentities(“b@chris85是的,我已经纠正了:)@fusion3k,我从一些api中获得了“bHtmlenties()b@chris85是的,我已经更正:)@fusion3k,我得到“b