3

Basically I want to take something like:

textheretextheretexthere
<table>
<tr><td>heres some stuff</td><td>and some morestuff</td></tr>
</table>
moretextheremoretexthere

and remove all the texthere and moretext here just leaving the table

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
user639705
  • 33
  • 4

3 Answers3

1

You need to find <table> position with strpos and then use substr to remove the text to this point.and then the same for </table>

$string = 'textheretextheretexthere<table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere';

$table_pos = strpos($string,'<table>');
$string = substr($string,$table_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>moretextheremoretexthere

$endtable_pos = strpos($string,'</table>')+8;//added 8 so i wont exclude </table>
$clean_string = substr($string,0,$endtable_pos);
//Your string now is <table><tr><td>heres some stuff</td><td>and some morestuff</td></tr></table>

Of course this is not perfect at all,i know but you got the hint,you can work on improving it and maybe end up with a function that helps you solve your problem.

Paris Liakos
  • 2,131
  • 3
  • 22
  • 23
0
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';    

echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
Cin Sb Sangpi
  • 687
  • 3
  • 7
  • 22
  • that would remove the tags yes, i am looking to remove any and all text before and after an html block that has been stripped, sorry i should have been more clear – user639705 Jul 11 '11 at 15:58
-1

This is would be the easiest way.

http://yelotofu.com/2008/08/jquery-outerhtml/

bitbandit
  • 409
  • 5
  • 10