SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace or str_replace or eregi_replace?

    12/13/94 13:50
    04H23H1998 16:12
    04c23c1998 16:12
    04!23!1998 16:12

    These are just a few of the possible patterns in a huge table of text entries. Most will have a random character for the date delimiter, though some use a / like normal. I need to place a <p> before each of them, while preserving the existing delimeter.

    Code:
    ereg("([0-9]{2}).([0-9]{2}).([0-9]{2}).([0-9]{2}):([0-9]{2})",$text,$regs)
    
    /*
    
    ([0-9]{2})  == match any two digits
    .                 == match any character (wildcard)
    ([0-9]{2})  == match any two digits
    .                 == match any character (wildcard)
    ([0-9]{2})  == match any two digits
    .                 == match any character (wildcard)
    ([0-9]{2})  == match any two digits
    :                 == match a colon only
    ([0-9]{2})  == match any two digits
    
    */
    The preceding code works fine wth ereg, but it only sees the first instance of the search criteria.

    I tried putting the same expression in preg_replace, but that returned the following error:

    Warning: Unknown modifier '.' in /var/www/html/test.php on line 116

    I really don't know which function I should be using, any and all help is greatly appreciated.
    - Mike
    http://www.georgiaoffroad.com

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you probably got the error with preg because you didn't use a pattern delimiter, such as //. e.g. /pattern/

    you definitely want to use preg_replace().

    let's see what kind of pattern you need. the date delimiter can be anything except a number? and the time delimiter is always a :? the year can be 2-4 numbers? do you need a <p> in front of the time delimiter too? i hope this pattern will work:

    Code:
    $txt = preg_replace('/(\d{2})(\D\d{2})(\D\d{2,4})(.\d{2})(:\d{2})/', '$1<p>$2<p>$3<p>$4<p>$5', $txt);
    \d matches any number (same as [0-9] in your code)
    \D matches anything that's not a number (same as [^0-9]).

    if your version of PHP is less than 4.0.4, replace the $n parts with \n
    Last edited by DR_LaRRY_PEpPeR; Apr 5, 2002 at 16:22.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Mucho thanks Dr Larry Pepper. Does the delimeter always have to be a / or are there others available as well (not that this affects anything, I'm just curious).
    - Mike
    http://www.georgiaoffroad.com

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    from the preg manual page:

    Any character can be used for delimiter as long as it's not alphanumeric or backslash (\).
    there ya go.


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
  •