Click to See Complete Forum and Search --> : advanced split string by skipping some text


MasterTom
09-04-2003, 06:36 AM
Hi there,

I want to split a string but in a more advanced way...

So what do i want to do?
I want to split ALL the TEXT between "<table>" and "</table>" from the string... so i can edit the remaining text (string/array)...
and then finally past them back together again...

I know i can use 'explode()' and 'implode()', but as you might understand... when a user defines more than 1 table... i should make an 'explode()' and 'implode()' sequence... to cover all the text...

Note:
I KNOW there's a way to do what i want... but i cant remember what is was... can you??

So basically...
I want to split a string in a way... that ALL the text between a "<table>" and "</table>" are used to split... in a way... that i can paste the splitted items WITH the text between the table-tags back together...

tnx... for reading/helping...

pyro
09-04-2003, 07:51 AM
Try something like this:

<?PHP
$str = "<table><tr><td>Tabular Data</td></tr><tr><td>More Tabular Data</td></tr></table>";

preg_match("/(\<table\>)(.*)(\<\/table\>)/",$str, $matches);
echo $matches[1]; //<table>
echo $matches[2]; //table contents
echo $matches[3]; //</table>

?>

MasterTom
09-04-2003, 12:41 PM
but ehm...

WHAT IF... I put more than 1 (!!) tables on the page...

thát was my real problem.. cause then the code should be dinamical... which would mean... a lot of code... i think...

but i hoped that anyone had an easier way...

does anyone?

pyro
09-04-2003, 07:16 PM
Do you mean nested tables? If so, you shouldn't be using nested tables anyway -- read up on CSS. If you mean more than one table per page, just split it at each table, and then use the code I posted above.

Kr|Z
09-05-2003, 08:22 AM
If you mean you want to find all the <table></tables>, instead of just the first one use preg_match_all() instead og preg_match().