EchoFool Posted January 6, 2009 Share Posted January 6, 2009 I have a table saved in a variable like this: <?php $var = '<td><img src="../images/med_1.gif" height=24 width=31></td> <td><img src="../images/med_2.gif" height=24 width=31></td> <td><img src="../images/med_3.gif" height=24 width=31></td> <td><img src="../images/med_4.gif" height=24 width=31></td> <td><img src="../images/med_1.gif" height=24 width=31></td> <td><img src="../images/med_1.gif" height=24 width=31></td> '; ?> And im looking for a way to while loop the variable to split it up to grab the source of the images in the same order.. so the outcome shows: And each time one is taken out of the variable it is inserted into a table in order as they come if that makes sense: row1 = ../images/med_1.gif row2 = ../images/med_2.gif row3 = ../images/med_3.gif row4 = ../images/med_4.gif row5 = ../images/med_1.gif row6 = ../images/med_1.gif Any ideas how i can do this.. in php ? Link to comment https://forums.phpfreaks.com/topic/139613-solved-strip-sentence-to-variables/ Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 I am sure you can do with regex, but yea this should work too. <?php $var = '<td><img src="../images/med_1.gif" height=24 width=31></td> <td><img src="../images/med_2.gif" height=24 width=31></td> <td><img src="../images/med_3.gif" height=24 width=31></td> <td><img src="../images/med_4.gif" height=24 width=31></td> <td><img src="../images/med_1.gif" height=24 width=31></td> <td><img src="../images/med_1.gif" height=24 width=31></td> '; $vars = split("</td>", $var); foreach ($vars as $var) { list(,$var) = split('src="', $var); list($var) = split('"', $var); $row[] = $var; } print_r($row); ?> Should work, unless I got the split function parameters mixed up. But yea. Link to comment https://forums.phpfreaks.com/topic/139613-solved-strip-sentence-to-variables/#findComment-730415 Share on other sites More sharing options...
EchoFool Posted January 6, 2009 Author Share Posted January 6, 2009 Kinda works but i get this: Notice: Undefined offset: 1 in med.php on line 34 What does this error mean ? Its relating it to this line: list(,$var) = split('src="', $var); Link to comment https://forums.phpfreaks.com/topic/139613-solved-strip-sentence-to-variables/#findComment-730428 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Try using this...the array_pop will get rid of the extra data that does not get parsed. <?php $var = '<td><img src="../images/med_1.gif" height=24 width=31></td> <td><img src="../images/med_2.gif" height=24 width=31></td> <td><img src="../images/med_3.gif" height=24 width=31></td> <td><img src="../images/med_4.gif" height=24 width=31></td> <td><img src="../images/med_1.gif" height=24 width=31></td> <td><img src="../images/med_1.gif" height=24 width=31></td> '; $vars = split("</td>", $var); array_pop($vars); $row = array(); foreach ($vars as $var) { list($x,$var) = split('src="', $var); list($var) = split('"', $var); $row[] = $var; } print_r($row); ?> Link to comment https://forums.phpfreaks.com/topic/139613-solved-strip-sentence-to-variables/#findComment-730433 Share on other sites More sharing options...
EchoFool Posted January 6, 2009 Author Share Posted January 6, 2009 works a treat thank you! Link to comment https://forums.phpfreaks.com/topic/139613-solved-strip-sentence-to-variables/#findComment-730515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.