SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pattern Matching. Are conditional replacement strings possible?

    What I am looking for is something that will turn:
    34s
    into:
    -34

    and:
    34n
    into
    34

    So I want the '-' in the replacement string to be conditional on whether the match uses n or s. This is really being done for a coordinate pairing, but if this is doable, an easier example might help make it more approachable.

    I can do it in two steps, by first getting all the matching data into an array, then parsing it, assembling it into strings for a simpler replace on the second pass.

    If the conditional replacement is not possible, what is the fastest simple string replacement function in php? Once I have the array of matches, I could simply setup a simple string replacement with no additional pattern matching if it was faster. ie "34s" to "-34". For an idea of scope, I would be doing this on many pages that have about 1-2 small screens of text.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    header
    ('content-type: text/plain');

    $subject 'So 32366s 5665n 616161s 16161n 654646s 4646146n';

    $result preg_replace('/(\d+)(s|n)/ei''(("\2" == "s") ? "-" : "") . "\1"'$subject);
    print 
    $result;
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice, I see what you did there, didn't know you could do that in the replacement string.

    I see in the pattern string you have 'i' which allows it to be case sensitive, but a capital S will not match the conditional in the replacement string...

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well will have to add more to the condition or strtolower.

    PHP Code:
    $result preg_replace('/(\d+)(s|n)/ie''((strtolower("\2") == "s") ? "-" : "+") . "\1"'$subject);
    # OR
    $result preg_replace('/(\d+)(s|n)/ie''(("\2" == "s" || "\2" == "S") ? "-" : "+") . "\1"'$subject); 
    Sadly PHP doesn't seem to support \u2 \l2 \i2 \f2
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much. What would this conditional syntax be called? An inline conditional? I assume it is part of php syntax instead of being part of preg_replace statement?

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    the e modifier put preg_replace into eval mode. From the manual:
    e (PREG_REPLACE_EVAL) If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string. Single quotes, double quotes, backslashes and NULL chars will be escaped by backslashes in substituted backreferences.
    Only preg_replace() uses this modifier; it is ignored by other PCRE functions.
    Note: This modifier was not available in PHP 3.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using 'e' is not "recommended" in most cases, because it's slow and can cause security problems. I'd suggest preg_replace_callback.


Bookmarks

Posting Permissions

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