Put in an array...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

Put in an array...

Post by revbackup »

Hi everyone!!!!

Given this variable with this value:

Code: Select all

$variable = '
<table>
<tr>
<td> 
<span onmouseover="_tipon(this)" onmouseout="_tipoff()">
<span class="google-src-text" style="direction: ltr; text-align: left">
1236791960OSDR
</span>
1236791960OSDR
</span>
</td>
 
<td> 
<span onmouseover="_tipon(this)" onmouseout="_tipoff()">
<span class="google-src-text" style="direction: ltr; text-align: left">
Super Administrator
</span>
Super Administrateur
</span>
</td>
</tr>
</table>';
 

how can I put the ones in blue [inside a <td>....</td>] in an array....???
so that array[0] contains the <span onmouseover="_tipon(this)" onmouseout="_tipoff()">
<span class="google-src-text" style="direction: ltr; text-align: left">
1236791960OSDR
</span>
1236791960OSDR
</span>

and array[1] contains <span onmouseover="_tipon(this)" onmouseout="_tipoff()">
<span class="google-src-text" style="direction: ltr; text-align: left">
Super Administrator
</span>
Super Administrateur
</span>
....
Last edited by Benjamin on Tue Jun 30, 2009 10:38 am, edited 1 time in total.
Reason: Added [code=php] tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Put in an array...

Post by Mark Baker »

Code: Select all

 
$result = array();
$split1 = explode('<td>',$variable);
foreach($split1 as $split) {
   $split2 = explode('</td>',$split);
   $result[] = $split2[0];
}
print_r($result)
 
I'm sure somebody will be along shortly with a regular expression to do the same thing in just one line of code
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: Put in an array...

Post by BornForCode »

The regex expression is:

Code: Select all

preg_match_all("/(?s)<td>(.+?)<\/td>/", $variable, $matches, PREG_PATTERN_ORDER);
And you will find what you need in the $matches array :)
Gimme a candy, gimme a candy
Last edited by Benjamin on Tue Jun 30, 2009 10:38 am, edited 1 time in total.
Reason: Added [code=php] tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Put in an array...

Post by Mark Baker »

BornForCode wrote:Gimme a candy, gimme a candy
Candy duly given.... remember to displose of the wrapper in a waste bin
revbackup
Forum Commoner
Posts: 29
Joined: Tue Jun 09, 2009 1:52 am

Re: Put in an array...

Post by revbackup »

hi guys!!
thanks for the BIG help!!!!
:D
Post Reply