[PHP] trying to match the front and end...

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default RE: [PHP] trying to match the front and end...

    On 08 August 2003 15:39, Dan Joseph wrote:
    > I've searched the high heavens for a method of doing this...
    > Here's what
    > I'm doing... First, the code..
    >
    > $middlenum = preg_replace("/^".$this->start_num."/", "",
    > $this->ach_acct_num); $middlenum =
    > preg_replace("/".$this->end_num."$/", "", $middlenum);
    >
    > 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?
    $middlenum = preg_replace("/^${this->start_num}(.*)${this->end_num}$/", '$1', $this->ach_acct_num);

    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: [email]m.ford@lmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford Guest

  2. Similar Questions and Discussions

    1. Front End SQL Statement
      Does anyone know a SQL statement that can run a macro or a module within ACCESS? -or- A CF Command/Tag that can run a macro or module within...
    2. Anyway to run a DB Macro from a CF web front end?
      Good morning, I was wondering if anyone knew of a way i could click a button on a CF web page which would run a query in our ACCESS DB? anyone...
    3. Enlarge the front of a car?
      I have a shot of a car that is almost coming directly at the viewer. You can actually see the side of the car, but very slightly. I would like to be...
    4. Minus Front
      I hope this isn't an overly stupid question, but how do I "minus front" in AI 11.0? In 9.0 and 10.0, there's a command in the Pathfinder palette...
    5. IE5 or Front Panel ?????
      Gang, We have a machine running Solaris 2.6 on which I installed Internet Explorer 5. I install it as root and it runs fine. It starts by...
  3. #2

    Default Re: [PHP] trying to match the front and end...

    From: "Dan Joseph" <djoseph@duhq.us>
    > I've searched the high heavens for a method of doing this... Here's what
    > I'm doing... First, the code..
    >
    > $middlenum = preg_replace("/^".$this->start_num."/", "",
    > $this->ach_acct_num);
    > $middlenum = preg_replace("/".$this->end_num."$/", "", $middlenum);
    >
    > 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?
    $new_number =
    preg_replace('/^'.$this->start_num.'([0-9]+)'.$this->end_num.'$/','\\1',$old
    _number);

    ---John Holmes...

    Cpt John W. Holmes Guest

  4. #3

    Default RE: [PHP] trying to match the front and end...

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

    -Dan Joseph

    Dan Joseph Guest

  5. #4

    Default Re: [PHP] trying to match the front and end...

    Dan Joseph wrote:
    >>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, 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?
    Should be:

    $new_number =
    preg_replace(
    '/^('.$this->start_num.')?([0-9]+)('.$this->end_num.')?$/',
    '\\2',
    $old _number);

    --
    ---John Holmes...

    Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]

    PHP|Architect: A magazine for PHP Professionals – [url]www.phparch.com[/url]




    John W. Holmes Guest

  6. #5

    Default RE: [PHP] trying to match the front and end...

    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: [email]m.ford@lmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford Guest

  7. #6

    Default Re: [PHP] trying to match the front and end...

    Dan Joseph wrote:
    >>Question:
    >>Where is this number coming from? Couldn't you just use a substr() based
    >>upon it's length and not deal with a regular expression?
    >
    >
    > Its a bank account number coming from a database. We're reformatting it
    > for ACH processing. The number could be:
    >
    > 23408234980423
    >
    > with a rule of remove 12453 from beginning, and remove 0423 from the end,
    > that would leave us with 2340823498. The rule could also read to remove 234
    > from the beginning, and 43985 from the end, leaving us 08234980423.
    >
    > The length of the number is also dynamic.
    >
    > I guess I just found it easier to go over it with a couple regular
    > expressions, then got to thinking maybe I could combine it into one. What
    > suggestion would you have?
    $start_length = strlen($this->start_num);
    if(substr($number,0,$start_length) == $this->start_num)
    { $number = substr($number,$start_length); }

    $end_length = 0 - strlen($this->end_num);
    if(substr($number,$end_length) == $this->end_num)
    { $number = substr($number,0,$end_length); }

    Not tested, mind you. I know what you're saying, too. How is _all_ of
    that code better(worse?) than one simple regex? Benchmark it and see.
    You'll be surprised how a lot more code with simple string functions
    will be considerably faster than a complex regular expression. Your
    results may vary, though. :)

    --
    ---John Holmes...

    Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]

    PHP|Architect: A magazine for PHP Professionals – [url]www.phparch.com[/url]




    John W. Holmes Guest

  8. #7

    Default RE: [PHP] trying to match the front and end...

    Hi,
    > Not tested, mind you. I know what you're saying, too. How is _all_ of
    > that code better(worse?) than one simple regex? Benchmark it and see.
    > You'll be surprised how a lot more code with simple string functions
    > will be considerably faster than a complex regular expression. Your
    > results may vary, though. :)
    Ahh, you do have a very valid point there.

    I tweaked your regex as stated in your follow up post, and mike's, they
    both work now. I'm gonna test a non-regex method. Performance is a factor
    in this. I'll post my results in a day or two when I get it situated.

    Thanks to you both!

    -Dan Joseph

    Dan Joseph Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139