Re: Regular Expression Query
| From: | Rasmus Lerdorf | Date: | Mon, 08 Jan 2001 02:39:10 +0000 |
| Subject: | Re: Regular Expression Query | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-33160@lists.php.net to get a copy of this message | ||
I answered this days ago.
Try this exact script:
$in = "Blah blah (Reference #123) blah blah";
$out = ereg_replace("\(Reference #([0-9]+)\)","\\1",$in);
echo $out;
This prints:
Blah blah 123 blah blah
Is that not exactly what you are looking for?
-Rasmus
On Sat, 6 Jan 2001, Cybercandy wrote:
> Thanks for those two points. I still haven't cracked this.
>
> I'm trying to break the problem down so I've tried the following
> example:
>
> $semifile2 = ereg_replace("\#39","YES",$semifile);
>
> But where there is a string: #39 it doesn't replace it with the
> word YES, however if I remove \# then it does replace
> 39 with YES
>
> Thanks for the help so far.
>
> Allan
> www.cybercandy.co.uk
>
>
> ----- Original Message -----
> From: "Rasmus Lerdorf" <rasmus@php.net>
> To: "Cybercandy Ltd" <allan@cybercandy.co.uk>
> Cc: <php-general@lists.php.net>
> Sent: Saturday, January 06, 2001 6:53 PM
> Subject: Re: [PHP] Regular Expression Query
>
>
> > > I think the most difficult thing I've encountered so far in trying to
> > > learn PHP, as a novice programmer, is regular expressions.
> > >
> > > I'm trying to use ereg_replace on a string containing a large quantity
> > > of data taken from a webpage which I then parse using
> > > the explode function.
> > >
> > > One regularly occuring string is: (Reference #xxx)
> > >
> > > Where xxx is a number of up to 3 digits.
> > >
> > > I want to replace this string using back references to just xxx
> > > and I've tried using the following:
> > >
> > > $out = ereg_replace("\(Reference #.([0-9]+).\)","\\1",$in);
> > >
> > > I just can't suss why this doesn't work.
> > >
> > > Any pointers anyone?
> >
> >
> > What are those .'s for in your regex?
> >
> > Just make it: "\(Reference #([0-9]+)\)"
> > and I bet it will work.
> >
> > with the .'s there you would only get the middle digit of your xxx
> >
> > -Rasmus
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> > For additional commands, e-mail: php-general-help@lists.php.net
> > To contact the list administrators, e-mail: php-list-admin@lists.php.net
> >
> >
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>