我正在尝试分解'ul'以获取文本。下面的代码,帮助,谢谢。
<?php
$html="<ul id='get_user' class='get_user'>1,2,3,4</ul>";
$first_explode=("<ul id='get_user' class='get_user'>",$html);
$second_explode=("</ul>", first_explode[1]);
$thrid_explode=(",", $second_explode[0]);
echo $thrid_explode[1]; //expecting 2
?>
仅此而已。
<?php
$html ="<ul id='get_user' class='get_user'>1,2,3,4</ul>";
$arrayUL = explode(",", strip_tags($html));
print_r($arrayUL);
?>
strip_tags手动
看看这个Stackloverflow答案:RegEx在HTML标签之间提取文本
正则表达式"/<([A-Za-z][A-Za-z0-9]*)'b[^>]*>(.*?)<'/'1>/"
将提取HTML标记中的文本。您可以使用php的preg_match:http://php.net/manual/en/function.preg-match.php
像这样:
$regex = '/<([A-Za-z][A-Za-z0-9]*)'b[^>]*>(.*?)<'/'1>/';
$string = '<div class="some-class" style="display:block;">Hello</div>';
preg_match($regex, $string, $matches);
print_r($matches);
总之,您希望使用正则表达式来解析比简单字符分隔符更复杂的内容。这里有一个方便的在线正则表达式测试仪:https://regex101.com/#pcre