Regex to remove span tags using php [duplicate]

大憨熊 提交于 2019-12-11 17:36:55

问题


Following is simple html i want to remove all span tag and just want 0.65 value. please someone help me on this.

<span class="smw smw-inline smw-basic smw-ct-default smw-visible" data-symbol="GSD.V" data-type="inline" data-refresh-frequency="5" data-source="live">
<span class="smw-market-data-field smw-field-l1" data-field="l1">0.65</span>
</span>

回答1:


You can remove all tag using preg_replace('/<[^>]*>/', '', $content);

And also you can remove only span tag preg_replace('/<span[^>]+\>/i', '', $content); using this:

<?php 
$content = '<div><span class="smw smw-inline smw-basic smw-ct-default smw-visible" data-symbol="GSD.V" data-type="inline" data-refresh-frequency="5" data-source="live">
<span class="smw-market-data-field smw-field-l1" data-field="l1">0.65</span>
</span></div>';

echo $string =  preg_replace('/<[^>]*>/', '', $content);

echo $new_string = preg_replace('/<span[^>]+\>/i', '', $content); 
?>



回答2:


I'm assuming PHP and Perl are similar enough for this ...

s{\</?span[^>]*/?>}{}gism

hth




回答3:


You can do it with preg_replace and regex:

<?php

$html = '<span class="smw smw-inline smw-basic smw-ct-default smw-visible" data-symbol="GSD.V" data-type="inline" data-refresh-frequency="5" data-source="live">
<span class="smw-market-data-field smw-field-l1" data-field="l1">0.65</span>
</span>';

$without_span = $html;
$without_span = preg_replace('#\n\r#', '', $without_span);
$without_span = preg_replace('#\n#', '', $without_span);

while(preg_match_all('#(<span.*?>)(.*?)(</span>)#', $without_span)) {  
  $without_span = preg_replace('#(<span.*?>)(.*?)(</span>)#', '$2', $without_span);
}

print_r($without_span);



回答4:


please try this below code.

 $string = '<span class="smw smw-inline smw-basic smw-ct-default smw-visible" data-symbol="GSD.V" data-type="inline" data-refresh-frequency="5" data-source="live">
<span class="smw-market-data-field smw-field-l1" data-field="l1">0.65</span>
</span>';

 echo strip_tags($string);


来源:https://stackoverflow.com/questions/44559607/regex-to-remove-span-tags-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!