News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Get string out of another string

Started by Cadish, October 14, 2003, 09:54:49 AM

Previous topic - Next topic

Cadish

I have a page, and there are lines in the code like this:

<td width="85%" class="txt">THIS</td>
<td width="85%" class="txt">THAT</td>
<td width="85%" class="txt">THERE</td>
<td width="85%" class="txt">ALWAYS</td>
<td width="85%" class="txt">NEVER</td>

How can i put the words 'THIS', 'THAT' , 'THERE', 'ALWAYS', 'NEVER' (they are always changing) into an array? I've tried with the command eregi, but that seems to be pretty hard to do...

Pls, can ya help me?
Thx a lot!!

chris

<?php

   $dummy      
'<td width="85%" class="txt">THIS</td>
<td width="85%" class="txt">THAT</td>
<td width="85%" class="txt">THERE</td>
<td width="85%" class="txt">ALWAYS</td>
<td width="85%" class="txt">NEVER</td>'
;

   
$pattern   '#<td width="85%" class="txt">(.+?)</td>#';
   
   echo 
'<pre>';
   if (
preg_match_all($pattern,$dummy,$matches))
      
print_r($matches);
   echo 
'</pre>';

?>

Cadish

Thanks a lot!!

I will give this a try...

Cadish

Ok, this works fine,
but sometimes the code is like this, so he doesn't add this into the string...

<td width="85%" class="txt">THIS</td>
<td width="85%" class="txt">TH
AT</td>
<td width="85%" class="txt">THERE</td>
<td width="85%" class="txt">ALWAYS</td>
<td width="85%" class="txt">NEVER</td>

chris

Replace

$pattern   = '#<td width="85%" class="txt">(.+?)</td>#';

with

$pattern   = '#<td width="85%" class="txt">(.+?)</td>#s';

and it works for multiple lines, too...

Advertisement: