Page 1 of 1

Put in an array...

Posted: Tue Jun 30, 2009 12:56 am
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>
....

Re: Put in an array...

Posted: Tue Jun 30, 2009 2:37 am
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

Re: Put in an array...

Posted: Tue Jun 30, 2009 4:25 am
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

Re: Put in an array...

Posted: Tue Jun 30, 2009 6:24 am
by Mark Baker
BornForCode wrote:Gimme a candy, gimme a candy
Candy duly given.... remember to displose of the wrapper in a waste bin

Re: Put in an array...

Posted: Wed Jul 01, 2009 5:15 am
by revbackup
hi guys!!
thanks for the BIG help!!!!
:D