RE: [PHP] trying to match the front and end...
| From: | Ford, Mike [LSS] | Date: | Fri, 08 Aug 2003 17:14:37 +0000 |
| Subject: | RE: [PHP] trying to match the front and end... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-158896@lists.php.net to get a copy of this message | ||
On 08 August 2003 17:39, Dan Joseph wrote:
> Hi,
>
> > > In a nutshell, what I want to do is chop off the front and the
> > > back. Example:
> > >
> > > I have: 1234567890
> > > I want: 456
> > >
> > > I have a start num and an end num. start = 123, end = 7890.
> > >
> > > This is working fine as I have it above, however I'd like to
> > > combine it into one regular express, instead of two. Can someone
> > > give me an example of matching the beginning and end at the same
> > > time?
>
> From Mike:
>
> > $middlenum =
> > preg_replace("/^${this->start_num}(.*)${this->end_num}$/", '$1',
> > $this->ach_acct_num);
>
> From John:
>
> > $new_number =
> >
> > preg_replace('/^'.$this->start_num.'([0-9]+)'.$this->end_num.'$/',
> '\\1',$old
> _number);
>
> The one that Mike gave didn't seem to do anything,
Oops!! Forgot to escape the final $ in the pattern -- should have read:
preg_replace("/^${this->start_num}(.*)${this->end_num}\$/", '$1',
$this->ach_acct_num);
> John's will work if it
> can match the beginning and the end successfully. I should probably
> explain myself further.
>
> Sometimes there won't be anything to replace at the front, and
> sometimes nothing at the end. So it'd still need to do the front
> and/or end wether or not they both exist.
>
> Is there a way to tweak these to do that?
If $this->start_num or $this->end_num contains the empty string (or even
NULL or FALSE), nothing will be interpolated into the string for those
values, so the above (corrected) pattern will still work. If
nothing-to-replace is signalled by something else, you may have a bit more
work to do...
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211