我想在<b>
标签中获取数据,例如:
<b>
<sup>1</sup>A, a
</b>
<b> ab </b>
<b><sup>2</sup>A</b>
我想从上面的标签中得到A,a
, ab
和A
。
但有时在数据行中没有<sup>
标记,如下所示:
<b>ab</b>
但是我想要得到数据ab
我试着集中在<b>
标签:
foreach($html->find('b') as $word) {
$words = $word->innertext;
echo $words.'<br>';}
但是当有<sup>
标签时,<sup>
标签内的文本也将被打印。如何不得到<sup>
标签内的数据?谢谢你
您可以使用clone()方法获取父元素中的文本,即<b></b>
,而忽略<sup></sup>
或其中的任何其他元素。
$('b')
.clone() //clone the element
.children() //select all childrens
.remove() //remove all the children
.end() //return to the matched element
.text(); //get the text
尝试:
<?php
$html = "<b>
<sup>1</sup>A, a
</b>
<b> ab </b>
<b><sup>2</sup>A</b>";
//remove all html tags without <sup>
$html = strip_tags($html,"<sup>");
//remove <sup> tag with its content
$html = preg_replace('#'<sup>[{'w},'s'd"]+'</sup>#', "", $html);
//remove 't, 'n and 'r (tabs, newline etc)
$html = str_replace(array("'t","'n","'r"),"",$html);
//also you can remove space from string
$html = str_replace(" ","",$html);
echo $html;
?>