Re: Text between two tags

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx


"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.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux