You are here: Re: [PHP] Text between two tags « PHP « IT news, forums, messages
Re: [PHP] Text between two tags

Posted by tg-php on 11/10/05 17:56

Great example.. haha.. I knew someone would pull some nice regex out for this one.. I suck at regex. hah

I still like my XML parsing fix, but Leonard's regex is probably better. hah

-TG

= = = Original message = = =

HI,

> Hello
> I'm trying to extract some text between two tags:

I am answering your question from the perspective that you are going
to have more than one column that you want the data from.

I would use this pattern and this code to test the pattern.
<?

$pattern="/(<td align=\"(.*)\" class=\"(.*)\">(.*)<\/td>)/";
$text='<td></td>
<td align="right" class="yfnc_tabledata3"></td>
<td></td>
<td align="right" class="yfnc_tabledata2"></td>
<td></td>
<td align="right" class="yfnc_tabledata1">1234</td>
<td></td> <td></td> <td></td> <td align="right" class="yfnc_tabledata5"></td> ';


preg_match_all($pattern, $text, $matches);

print_r($matches);

?>

This will give you:

[0] => Array
(
[0] => <td align="right" class="yfnc_tabledata3"></td>
[1] => <td align="right" class="yfnc_tabledata2"></td>
[2] => <td align="right" class="yfnc_tabledata1">1234</td>
)

[1] => Array
(
[0] => <td align="right" class="yfnc_tabledata3"></td>
[1] => <td align="right" class="yfnc_tabledata2"></td>
[2] => <td align="right" class="yfnc_tabledata1">1234</td>
)

[2] => Array
(
[0] => right
[1] => right
[2] => right
)

[3] => Array
(
[0] => yfnc_tabledata3
[1] => yfnc_tabledata2
[2] => yfnc_tabledata1
)

[4] => Array
(
[0] =>
[1] =>
[2] => 1234
)


THen you can loop through $matches[4] and you will get all of the
values in your row. $mathces[3] will give you your class names and
$matches[2] will give you the alignment and $matches[1] will give you
the whole <td blah blah blah /td> string.

You could explode on <tr> and get each new row of your table in a new
element in an array and repeat the process for your entire table.

Do note that my regular expression will only return columns with a
class name and align attribute.

You can use $matches[0] elements if you are needing to replace what is
there with other information.

I hope this helps. Please let me know if it does what you are asking
for as I am in need of good regular expression practice.

If you are needing to alter my regex some you can use
http://www.quanetic.com/regex.php to help.



--
Leonard Burton, N9URK
leonardburton@gmail.com


"The prolonged evacuation would have dramatically affected the
survivability of the occupants."

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация