问题
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