Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Replace only A-Z/0-9 using ereg_replace();

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default Replace only A-Z/0-9 using ereg_replace();

    I'm using a PHP version earlier than 5.3.0.

    I have a series of stock numbers on a page, that I need to detect and replace. The pattern of each stock number are letters and numbers, containing a period somewhere in between, and possibly a dash.

    The code I have works to detect the stock numbers, however they include the HTML tags if they are not separated by a space.

    PHP Code:
    $text "<strong>34D-23.48</strong>";
    $text ereg_replace("([a-zA-Z0-9]+.+[a-zA-Z0-9])",
                         
    "Stock # \\0"$text);

    echo 
    $text
    However, the resulting HTML outputs as this:
    Code:
    <Stock #strong>34D-23.48</strong>
    I can't separate the stock numbers, how do I get it so the output is like this?
    Code:
    <strong>Stock #34D-23.48</strong>
    Thanks in advance for the help. :]
    -Josh
    - Josh

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,194
    Thanks
    96
    Thanked 100 Times in 98 Posts

    Default

    Honestly, I am quite rusty when it comes to ereg_replace(). I am having some trouble remembering the syntax for ereg, which does differ somewhat from PCRE. ereg_replace() is the more processor heavy version of preg_replace() and has since been deprecated. I was thinking that it had been removed completely in more recent versions of php, but I have not found any reference to back that up, so I am probably mistaken there.

    I'm sure you could do the same thing with preg_replace

    Code:
    <?php
    $text = "<strong>34D-23.48</strong>"; 
    $text = preg_replace("/>\s?([\w.\-]{1,})\s?</", ">Stock # $1<", $text); 
    
    echo $text; 
    ?>
    \s? is for optional whitespace. \w is a word character, which is A-Za-z0-9 and the underscore. \- is for the dash, but since it is in a character class it needs to be escaped. the dot does not need to be escaped if it is within a character class. {1,} is a quantifier saying that there must be one or more of the character class.

    If you really want it in ereg_replace format, let me know and I'll see what I can come up with.

    The above script assumes that the stock number is between > and <.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    JShor (06-18-2010)

  4. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Yes, preg_replace() accomplishes the same thing. Thanks a million James, the code worked. :]
    - Josh

  5. #4
    Join Date
    Apr 2010
    Location
    UK
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes preg_replace() is a much better option if only purely based on speed. It depends how much it's called but i had a loop processing a lot of data and i timed both ereg and preg.. preg was faster by quite a few seconds!
    Last edited by djr33; 06-18-2010 at 10:50 PM.

  6. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Alright, that issue is solved, but I have one more minor problem. I have numbers which display as a period at the beginning or end of a set of characters. I want to exclude these from being replaced.

    For example, I will have .093 or 093., which are not supposed to be replaced. Can you modify the code so that it only replaces stock #'s with periods in the middle?

    Also, the majority of the stock #'s have a </td> tag right after the number. When I tried modifying the code to include that in the replacement, it gives me an error.

    This is the code;
    PHP Code:
    $text preg_replace("/>\s?([\w.\-]{1,})\s?</" "</td>"">Stock # $1<"$text); 
    Help is greatly appreciated. :]
    Last edited by JShor; 06-18-2010 at 07:14 PM.
    - Josh

  7. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Alright, I managed to figure it out. Thanks guys.
    - Josh

  8. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,194
    Thanks
    96
    Thanked 100 Times in 98 Posts

    Default

    I am having a difficult time with this one. What was the solution?
    To choose the lesser of two evils is still to choose evil. My personal site

  9. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,194
    Thanks
    96
    Thanked 100 Times in 98 Posts

    Default

    k, I got it.

    Code:
    <?php
    $text = "<strong>34D-23.48</strong>"; 
    $text=preg_replace('/>\s?([\w\-]{1,}\.?)(?=((?!<\/?strong\b|>|\.\s?<).)*<\/strong)/','>stock # $1',$text);
    echo "$text";
    ?>
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Still doesn't work. It will replace the stock number, but it also replaces ALL the strings which precede the </strong> tag. I'm stumped.

    When I have this code:
    PHP Code:
    $text "<strong>DontReplaceThis</strong>"
    I get this result:

    stock # DontReplaceThis

    It's not replacing JUST the string pattern with the decimal place.

    Once again, help is greatly appreciated. Thank you. :]
    - Josh

  11. #10
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,194
    Thanks
    96
    Thanked 100 Times in 98 Posts

    Default

    That is what it is supposed to do.

    Could you give some examples of what you want replaced and what you do not want replaced? The only thing I can think of is that I have not yet restricted it to match the string that is between the <strong> tags that also contains a dot in the middle somewhere.

    Basically it looks for all data between <strong> tags where the string is not preceded or appended with a dot. The string must begin with a letter or a number or underscore or dash and end with the same. periods can be in the middle of the string, but the period is optional.
    To choose the lesser of two evils is still to choose evil. My personal site

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
  •