Jump to content

Recommended Posts

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

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.

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.