Re: Match for titles using pregexp
| From: | Robert Cummings | Date: | Thu, 27 Dec 2007 11:47:00 +0000 |
| Subject: | Re: Match for titles using pregexp | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-266293@lists.php.net to get a copy of this message | ||
On Thu, 2007-12-27 at 13:53 +0300, OOzy Pal wrote:
> On Dec 27, 2007 12:40 PM, Robert Cummings <robert@interjinn.com> wrote:
> > On Thu, 2007-12-27 at 11:27 +0300, OOzy Pal wrote:
> > > more words/chars[<a href="#c_1" id="ids_1"
> > > title="Hello">2</a>]more words/chars
> > >
> > > How can I match for the title. In this case the word Hello using regexp?
> >
> > <?php
> >
> > $text = 'more words/chars[<a href="#c_1" id="ids_1"
> > title="Hello">2</a>]more words/chars';
> >
> > $title = null;
> > if( preg_match( '/title="([^"]*)"/Umi', $text, $bits ) )
> > {
> > $title = $bits[1];
> > }
> >
> > echo 'Title: '.$title."\n";
> >
> > ?>
> Wow, worked perfect. Thank you. I just made it (preg_match_all)
>
> more words/chars[<a href="#c_1" id="ids_1"
> title="Hello">2</a>]more words/chars
>
> How can I also find the number 2 in (...>2</a...) in the same sentence
> and made preg_match_all make array of two items
>
> Array
> (
> [0] => Hello
> [1] => 2
>
> )
<?php
$text = 'more words/chars[<a href="#c_1" id="ids_1"
title="Hello">2</a>]more words/chars';
$title = null;
if( preg_match(
'#<a[^>]*title="([^"]*)"[^>]*>([^<]*)</a>#Umi', $text,
$bits ) )
{
$title = $bits[1];
$content = $bits[2];
}
echo 'Title: '.$title."\n"
.'Content: '.$content."\n";
?>
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................