Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

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

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

    Default

    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.
    All of that statement is correct, except the period is not optional. I want only stock numbers replaced, and only stock numbers have a period. Therefore, the period is mandatory.
    - Josh

  2. #12
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Easy enough to fix:

    Code:
    <?php
    $text = "<strong>34D-2348</strong>";
    $text=preg_replace('/>\s?([\w\-]{1,}\.[\w\-]{1,})(?=((?!<\/?strong\b|>|\.\s?<).)*<\/strong)/','>stock # $1',$text);
    echo "$text";
    ?>
    Sorry, I think I was just being lazy. Be sure to test it out to see if there are any glitches I may have missed.

    Here is some small explanation of the PCRE tools I used.

    ? listed after a character type or match makes the preceding match optional.
    {1,} this means 1 or more.
    $1 is the first subpattern captured. subpatterns are numbered starting from the first opening parentheses from left to right with $0 as the entire pattern.

    ?= listed just inside a parentheses means that the following must come after what was listed previously. This is known as an assertion. Assertions are also a bit more complicated and do not follow the same rules as the Perl regular expression engine. This is one of the few differences from Perl regular expressions. Assertions can be nested, but the assertion cannot be repeated, for example I can't say '/p(?=p)*/' (?= is known as a lookahead. You can't have a lookahead and a lookbehind with PCRE, which is what made the regex a little tricky to create.

    Assertions is another way of saying "this MUST follow that" or "this MUST NOT follow that" or "this MUST precede that". Again, assertions can be nested as demonstrated above.

    You can read up more on assertions here, but I find the PHP manual of PCRE somewhat lacking and even more so now that all of the hundreds of very useful comments were removed.
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    I figured it all out, but there is one last problem. If I put quotes around $1, it doesn't encase the entire stock number, only the first part before the decimal place. Here is the code:

    PHP Code:
    $text preg_replace('/>\s?([\w\-]{1,}\.)/','>stock # "$1" ',$text); 
    Instead of getting stock # "34D-23.48", I get: stock # "34D-23."48.
    - Josh

  4. #14
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    With the script you are using you are only capturing up to the period with the exceptions we already discussed: it can't begin or end with a period and must have a period in the middle, it may or may not begin and/or end with a space, it is all wrapped in <strong> tags.

    With the one I posted you will capture everything between the <strong> tags and not just everything up to the period.
    Last edited by james438; 06-19-2010 at 12:55 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

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

    JShor (06-19-2010)

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

    Default

    Right, I used the code you last posted, works perfectly. It replaces all the stock numbers with containers (which is what I originally had intended). I really need to get used to using preg_replace() now that ereg_replace() is deprecated in the latest PHP versions. Thanks again. :]
    - Josh

  7. #16
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I am not sure of all the ins and outs of what you need the script for, but the following is better than what I posted earlier and easier to understand.

    PHP Code:
    <?php
    $text 
    "<strong>34D-2.348</strong>";
    $text preg_replace('/strong>\s?([\w\-]{1,}\.[\w\-]{1,})(?!\.)\s?<\/strong/','strong>stock # $1</strong',$text);
    echo 
    "$text";
    ?>
    Well, maybe not better, but I find it easier to understand. They both do pretty much the same thing, but this one is somewhat stricter about being between <strong> tags.
    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
  •