Remove <br /> and &nbsp; between table tag

<?php
$myText=[B]"[/B]This[COLOR="Blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]before[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table[COLOR="Blue"]<br />[/COLOR]
<table>[COLOR="Red"]<br />
&nbsp;&nbsp;[/COLOR]<tr>[COLOR="red"]<br />
&nbsp;&nbsp;&nbsp;&nbsp;[/COLOR]<td>This[COLOR="Blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]in[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]
table</td>[COLOR="red"]<br />
&nbsp;&nbsp;[/COLOR]</tr>[COLOR="red"]<br />[/COLOR]
</table>[COLOR="Blue"]<br />[/COLOR]
This[COLOR="Blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]after[COLOR="Blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table.";
?>

I have myText like the above.

I like to change the value of $myText like the below.

This[COLOR="blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]before[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table
<table><tr><td>This[COLOR="blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]in[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table</td></tr>
</table>[COLOR="Blue"]<br />[/COLOR]
This text[COLOR="Blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]after[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table.";

use strip_tags() function.
for more info on this::
http://in2.php.net/manual/en/function.strip-tags.php

strip_tag make it remove all html tags.

But I like to leave all tags.
Instead of removing tags,
I like to remove <br /> and   between <table> and <tr>
I like to remove <br /> and   between <tr> and <td>
I like to remove <br /> and   between </td> and </tr>
I like to remove <br /> and   between </tr> and </table>

use strip_tags() function like this:::
$txt=strip_tags($txt,'<br/> ');

This will allow what you want to do…

I have the code below at http://dot.kr/x-test/strip_tag1.php .

<?php
$txt="This text is before the table<br />
<table><br />
&nbsp;&nbsp;<tr><br />
&nbsp;&nbsp;&nbsp;&nbsp;<td>This text is in the table</td><br />
&nbsp;&nbsp;</tr><br />
</table>
This text is after the table.";

[COLOR="Red"]$txt=strip_tags($txt,'<br/>&nbsp;')[/COLOR];
echo $txt;
?>

The following is the result of the code above.

[b]result[/b]

This text is before the table

&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;This text is in the table

&nbsp;&nbsp;

This text is after the table.

My target result below is, I think, quite different from the result above.

[b]target result[/b]

This[COLOR="blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]before[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table
[COLOR="Red"]<table><tr><td>[/COLOR]This[COLOR="blue"]&nbsp;[/COLOR]text[COLOR="blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]in[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table[COLOR="Red"]</td></tr>
</table>[/COLOR][COLOR="Blue"]<br />[/COLOR]
This text[COLOR="Blue"]&nbsp;[/COLOR]is[COLOR="blue"]&nbsp;[/COLOR]after[COLOR="blue"]&nbsp;[/COLOR]the[COLOR="blue"]&nbsp;[/COLOR]table.";

means, you can generate sub-strings by finding place holders on each and then apply to this function…

I am afraid that I don’t understand what you mean.

Could you tell me a little more detail, please?

What is place holders?
How can I make it?

find the positions of <table> and </table> in the original text…
After that you can get two sub-strings from original text as::::
(1).Get sub-string of original text from starting position to the <table> position and
(2).Get sub-string of original text from </table> position to the end position.

then on those two new strings, you can apply the strip_tags() function…

strpos($txt,'<table>') 
strpos($txt,'</table>')

I found the position of <table> is 37 and the position of </table> is 167 with the code above.

What two sub-strings should I get from original text?

I think there are three sub-strings like the follow.

from the first to 37, 
from 37 to 167 
and from 168 to the last

for that 3 sub-strings, you can make 3 strip_tag functions based on your requirement.
Means 2nd string contains table data, for that you can write strip_tag() function what tags to allow on the body of table data…
Similarly, you can write for other strings also…

When in doubt, read the manual. [fphp]strip_tags[/fphp] :slight_smile:

<?php
$txt="This text is before the table<br />
<table><br />
&nbsp;&nbsp;<tr><br />
&nbsp;&nbsp;&nbsp;&nbsp;<td>This text is in the table</td><br />
&nbsp;&nbsp;</tr><br />
</table>

This text is after the table.";

$openTablePos=strpos($txt,'<table>');
$closeTablePos=strpos($txt,'</table>');

$preTable=substr($txt,0,$openTablePos);
$midTable=substr($txt,$openTablePos,$closeTablePos-$openTablePos+8);
$postTable=substr($txt,strLen($preTable)+strLen($midTable),
strLen($txt)-strLen($preTable)-strLen($midTable) );

$tableTxt=strip_tags($midTable);

$tdTxt=str_replace("&nbsp;","",$tableTxt);


$tableWITHtxt="<table><tr><td>".$tdTxt."</td></tr></table>";

echo $preTable.$tableWITHtxt.$postTable;
?>

The code above gets my target result.
It works fine.

Now I like to ask you a little more developed question.

How can I make my target result below with myText below ?

$[B]myText[/B]="This text is before the table<br />
<table><br />
&nbsp;&nbsp;<tr><br />
&nbsp;&nbsp;&nbsp;&nbsp;<td>This [COLOR="Red"]<span style='color:red'>[/COLOR]text[COLOR="red"]</span>[/COLOR]
is in the table</td><br />
&nbsp;&nbsp;</tr><br />
</table>

This text is after the table.";

[b]target result[/b]

This text is before the table<br />

<table><tr><td>

This [COLOR="Red"]<span style='color:red'>[/COLOR]text[COLOR="red"]</span[/COLOR]> is in the table

</td></tr></table>

This text is after the table.

Can’t the function strip_tag() do this, can it?

This is also same.
By using strip_tags() function, you can get the result.
Store that content in a variable, and pass it to the strip_tags() function as a first parameter, and specify the tags what you want to be allowed as the second parameter.