RE: Need help with RegEx

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

 



On 11 December 2006 19:43, Michael wrote:

> At 08:29 AM 12/11/2006 , Brad Fuller wrote:
> > 
> > The example provided didn't work for me.  It gave me the same
> > string without anything modified.
> 
> You are absolutely correct, this is what I get for not
> testing it explicitly :( My most sincere apologies to the OP
> and the list, there is an error in my example (see below for
> correction) 
> 
> **** I have cut and pasted from further down in the quoted
> message, for convenience ****
> > > Using the tags you describe here, and assuming the source html is
> > > in the variable $source_html, try this:
> > > 
> > > $trans_text = preg_replace("/(.*?)(<div id=result_box
> > > dir=ltr>)(.*?)(<\/div>)(.*?)^/s","$3",$source_html);
> 
> The End of string symbol ^ should not be included.

That's because ^ is not the end-of-string symbol -- it's the START-of-string symbol.  $ is the END-of string symbol.  But the OP doesn't need either of these symbols as he's not trying to match at the start or end of the string, and nor does he need your suggested leading and trailing (.*?) for the same reason.  Unless anchored with ^ and/or $, preg is perfectly happy to match in the middle of the subject string.

@Anthony: your pattern is fine -- it's what you're doing with it that's wrong.

On 11 December 2006 08:03, Anthony Papillion wrote:

> $trans_text = preg_match("\/<div id=result_box
> dir=ltr>(.+?)<\/div>/");
> 
> The problem is that when I echo the value of $trans_text variable, I
> end up with the entire HTML of the page.

I don't see how this is possible, since preg_match returns an integer telling you how many times the pattern matched -- which will be 0 or 1, since preg_match doesn't do multiple matches!  You also clearly haven't given us your actual call, since you've only included the pattern and not the subject string.

What you're after is the third argument to preg_match, which returns an array of matched text; so for:

    preg_match("/<div id=result_box> dir=ltr>(.+?)<\\/div>/", $orig, $matches);

$matches[0]  will return the entire match (everything from "<div " to "</div>"
$matches[1]  will return the first parenthesized expression, which is what you're looking for.

Note also the doubled backslash, since you need to pass a single backslash through to escape the / for preg_match.  As an alternative, I would strongly advise using a different delimiter, so that no escaping is needed; for instance:

    preg_match("#<div id=result_box> dir=ltr>(.+?)</div>#", $orig, $matches);

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

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