Title: Only leave some tags in a html string using PHP
Tags: php;simple-html-dom
Question: I am crawling a website with simple_html_dom and need the result that would be somewhere between ->innertext and ->plaintext.
For example, here is the source string:
<span lang="EN-CA"&gt;[28]<span style="font:7.0pt &amp;quot;Times New Roman&amp;quot;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
</span&gt;</span&gt;<span lang="EN-CA"&gt;The Canadian trade-marks regime is national in
scope.  The owner of a registered trade-mark, subject to a finding of
invalidity, is entitled to the exclusive use of that mark in association with
the wares or services to which it is connected throughout Canada.  Section 19 of
the <i&gt;Trade-marks Act</i&gt; provides:</span&gt;
I need to get rid of the span tags but not their contents (unless the span only contains &amp;nbsp;'s) but retain <i&gt;, <u&gt; and <b&gt;'s
So the result I'd like to achieve here would be a string:
[28] The Canadian trade-marks regime is national in scope.  The owner of a registered trade-mark, subject to a finding of invalidity, is entitled to the exclusive use of that mark in association with the wares or services to which it is connected throughout Canada.  Section 19 of the <i&gt;Trade-marks Act</i&gt; provides:

Here is the accepted answer: You can try the following lines of code:
<?php
$string = '<span lang="EN-CA"&gt;[28]<span style="font:7.0pt &amp;quot;Times New Roman&amp;quot;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;n
bsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; </span&gt;</span&gt;<span lang="EN-CA"&gt;The Canadian tr
ade-marks regime is national in scope. The owner of a registered trade-mark, subject to a finding of invalidity, is entitled to the exclusive u
se of that mark in association with the wares or services to which it is connected throughout Canada. Section 19 of the <i&gt;Trade-marks Act</i&gt;
provides:</span&gt;';
// Remove attributes within the <span&gt; tag, just for clarity's sake.
$string = preg_replace('/(<span ([^\&gt;]+)&gt;)/i', '<span&gt;', $string);
// Remove any spans that only contain &amp;nbsp;
$string = preg_replace('/<span&gt;([ ]|&amp;nbsp;)*<\/span&gt;/i', '', $string);
// Replace any consecutive span (opening or closing) tags with a space, to make
// clear the separation between one span and the next.
$string = preg_replace('/<(\/)?span&gt;<(\/)?span&gt;/i', ' ', $string);
// Remove any remaining any instances of opening or closing span tags.
$string = preg_replace('/<(\/)?span&gt;/i', '', $string);
print $string;
Note that I added an i after the slash for each regular expression, which gives you a case-insensitive search.  That's just in case you have some code that is <SPAN&gt; or <span&gt; or even <SpaN&gt;.
Granted, it's not a tightly compressed single line of regular expression code awesomeness.  But, I did it this way so that you can see the steps along the way.  You can put the print $string; line throughout to see the progression.  I was hoping this way of demonstrating the code to you would help you, in the long run, to get a better feel for how regular expressions and preg_replace can be used.
Comment for this answer: Thank you. This is just what I needed!

Here is another answer: This is what strip_tags is for:
echo strip_tags('<span&gt;strip me</span&gt; <i&gt;leave me alone</i&gt;', '<i&gt;');
//=&gt; strip me <i&gt;leave me alone</i&gt;

Here is another answer: you may try this .
echo stripcslashes('<span lang="EN-CA"&gt;[28]<span style="font:7.0pt &amp;quot;Times New Roman&amp;quot;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; </span&gt;</span&gt;<span lang="EN-CA"&gt;The Canadian trade-marks regime is national in scope. The owner of a registered trade-mark, subject to a finding of invalidity, is entitled to the exclusive use of that mark in association with the wares or services to which it is connected throughout Canada. Section 19 of the <i&gt;Trade-marks Act</i&gt; provides:</span&gt;');

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/10/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><simple-html-dom>', 'Title': 'Only leave some tags in a html string using PHP', 'ViewCount': 127, 'id': 21669311, 'user_AboutMe': None, 'user_AccountId': 934104.0, 'user_DisplayName': 'Uno Mein Ame', 'user_Reputation': 1020.0, 'user_Views': 115.0, 'user_id': 963369.0}}