Re: Regular expressions
| From: | Richard Lynch | Date: | Mon, 16 Oct 2006 17:42:43 +0000 |
| Subject: | Re: Regular expressions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-243194@lists.php.net to get a copy of this message | ||
On Sat, October 14, 2006 4:19 pm, Morten Twellmann wrote:
> I'm trying to understand these regular expressions, but I can't make
> them
> work...
>
> All I want to do, is to find the first occurrence of some text inside
> the
> HTML tags <h1> and </h1>.
>
> Example string: "<p>October 14, 2006</p><h1>Welcome to my
> homepage</h1><p>We're happy to announce...</p>"
>
> must return:
>
> "Welcome to my homepage"
>
>
> I tried with:
>
> preg_match('<h1\b[^>]*>(.*?)</h1>', "<h1>Welcome to my
> homepage</h1>",
> $matches, PREG_OFFSET_CAPTURE);
> print_r($matches);
>
> but got nothing...
>
> Can anyone tell me how to do this?
>
> (I tried the above expression in EditPad Pro 6 and it worked...!)
Download "The Regex Coach" and play with that -- It's not 100% the
same as PHP, and the escaping of backslashes for PHP isn't in it, but
it rocks for visual presentation of pattern/match
Your main problem is a lack of start/end delimiters for the pattern:
'|<h1[^>]*>(.*)</h1>|ims'
would probably be better.
| at beginning/end as pattern delimiters
i becase H1 and h1 are both the same tag in HTML
ms because newlines inside the text are okay
Take out the \b because I dunno what it does, but you don't need it.
.*? is kinda silly -- .* mean "0 or more characters", and ? means
"maybe" but putting them together has no added value, so lose the ?
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?