Re: extracting text - regex
| From: | Ashley Sheridan | Date: | Fri, 10 Apr 2009 11:20:10 +0000 |
| Subject: | Re: extracting text - regex | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291332@lists.php.net to get a copy of this message | ||
On Wed, 2009-04-08 at 17:14 +0200, Merlin Morgenstern wrote:
>
> George Larson wrote:
> > On Wed, Apr 8, 2009 at 9:13 AM, George Larson <george.g.larson@gmail.com>wrote:
> >
> >> I'm what you might consider rather green, myself.
> >> I certainly wouldn't use this code for production but if you're just
> >> debugging or something then how about something like this:
> >>
> >> <?php
> >> $handle = @fopen('page.htm', 'r');
> >> if ($handle) {
> >> while (!feof($handle)) {
> >> $eos = trim(fgets($handle, 4096));
> >> if (strpos($eos,"<div>") !== FALSE) { $echo_output = TRUE;
> >> }
> >> if (strpos($eos,"<\div>") !== FALSE) { $echo_output =
> >> FALSE; }
> >> if ($echo_output == TRUE) { echo $eos; }
> >> }
> >> fclose($handle);
> >> }
> >> ?>
> >>
> >>
> >>
> >>
> >> On Wed, Apr 8, 2009 at 8:30 AM, Per Jessen <per@computer.org> wrote:
> >>
> >>> Merlin Morgenstern wrote:
> >>>
> >>>> Hello,
> >>>>
> >>>> I am trying read text out of a text that is inbetween two divs.
> >>>> Somehow this should be possible with regex, I just can't figure out
> >>>> how.
> >>>>
> >>>> Example:
> >>>>
> >>>> <div id="test">
> >>>> bla blub
> >>>> </div>
> >>>>
> >>>> I would like to extract the text "bla blub" out of this example.
> >>>>
> >>> This might do the trick (not tested):
> >>>
> >>> preg_match( "/<div\s+[^>]*>([^<]*)<\/div>", $yourtext,
> >>> $match );
> >>> print $match[1];
> >>>
> >>>
> >>> /Per
> >>>
> >>>
> >>> --
> >>> Per Jessen, Zürich (16.8°C)
> >>>
> >>>
> >>> --
> >>> PHP General Mailing List (http://www.php.net/)
> >>> To unsubscribe, visit:
> >>> http://www.php.net/unsub.php
> >>>
> >>>
> > Sorry about the top-post. I forgot. :)
> >
>
>
> Thank you everbody. I figured it out without regex. It's not for
> production, just testing:
> $pos_1 = strpos($contents, $word1);
> $pos_2 = strpos($contents, $word2, $pos_1);
> $text = strip_tags(substr($contents, $pos_1, $pos_2 - $pos_1));
>
> That works as well.
>
>
>
Or...
strip_tags() which would work perfectly well for the example excerpt you
gave us!
Ash
www.ashleysheridan.co.uk