#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5

    Arrow Replace text between HTML tags


    Hello everyone,
    My name is Roi and I need your help.
    Hope you will be able to help me-
    I wrote a script that take variable that contain HTML code,. it replace the exact word "php" (no matter if it is contain capital letters) with the word "asp"
    for example, if the variable contain:
    PHP Code:
    <a href='php.com'>myphp</abest <b>php</bwebsite <h1>PhP!!!</h1php and myphp or phpme - <u>php!</u>! 
    the result will be
    PHP Code:
    <a href='asp.com'>myphp</abest <b>asp</bwebsite <h1>asp!!!</h1aspand myphp or phpme - <u>asp!</u>! 
    well, the problem is that it althouth replace the letters inside the THML tags and because of that the links changed...
    Here is my code:
    PHP Code:
        function keepcase($word$replace) {
          
    $replace[0] = (ctype_upper($word[0]) ? strtoupper($replace[0]) : $replace[0]);
          
          return 
    $replace;
        }

        
    $text strtolower(file_get_contents($folder.$file));
        
    $replace "asp";
        
    $word "php";
        
    $output preg_replace('/\b' preg_quote($word) . '\b/ei'"keepcase('\\0', '$replace')"$text);
      
        echo 
    $output
    What should I change if I want the replacements to be only on the text between the HTML tags?

    Thank you in advance,
    Roi.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    This is a very difficult problem that cannot easily be solved using a regular expression. Your best bet is something like:
    PHP Code:
    preg_replace("/>([^<]*)(" preg_quote($word) . ")([^<]*)</ie""'>\\1' . keepcase('\\2', '{$replace}') . '\\3<'"$text); 
    That still won't get all the examples, but it's closer.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5
    Hi ManiacDan,
    Thank for your code!

    I tried it, and as you said - it's good, but still have some problems - for now it don't replace the code inside the <a> tag, but it don't ignore 2 things that it supposed to ignore:
    a. it supposed to ignore words that not exactly like the wey words. for example - it supposed to ignore the word "phpme", but istead it replace it to "aspme".
    b. It supposed to replace the word "PHP" even if there are capital letters, for example - "pHP".

    My input was:
    PHP Code:
    <a href='php.com'>myphp</abest <b>php</bwebsite <h1>PhP!!!</h1php AND myphp or phpme - <u>php!</u>! <img src="" alt="great Php" /><a href='php.com'>php</a
    and the output was:
    PHP Code:
    <a href='php.com'>myasp</abest <b>asp</bwebsite <h1>Asp!!!</h1php AND myphp or aspme - <u>asp!</u>! <img src="" alt="great Php" /><a href='php.com'>asp</a
    Well, the output supposed to be:
    PHP Code:
    <a href='php.com'>myphp</abest <b>asp</bwebsite <h1>Asp!!!</h1asp AND myphp or phpme - <u>asp!</u>! <img src="" alt="great Php" /><a href='php.com'>asp</a
    Any chance you'll take a look at this one more time?
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Added \b:

    PHP Code:
    function keepcase($a$b) { return $b; }
    $text " <a href='php.com'>myphp</a> best <b>php</b> website <h1>PhP!!!</h1> php AND myphp or phpme - <u>php!</u>! <img src=\"\" alt=\"great Php\" /><a href='php.com'>php</a>    ";
    $word 'php';
    $replace 'asp';
    echo 
    htmlentities(preg_replace("/>([^<]*)(\b" preg_quote($word) . "\b)([^<]*)</ie""'>\\1' . keepcase('\\2', '{$replace}') . '\\3<'"$text));  
    die(); 
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5
    I think its the best result we got for this in the past 3 days...

    I made a test and added one more "php" to the text (after the <b>) :
    PHP Code:
    $text "<a href='php.com'>php</a> best <b>php phP MYPHP</b> website <h1>ever!!!</h1> php and myphp or phpme - <u>php!</u>!"
    The script supposed to changed it to "asp" but it didn't...
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    That still won't get all the examples
    You'll have to write another for the case of content at the "edge" of the string, without any tags to be between. You can either write two more replacements (beginning and end) or you can "cheat" by doing:
    PHP Code:
    function keepcase($a$b) { return $b; }
    $text " <a href='php.com'>myphp</a> best <b>php</b> website <h1>PhP!!!</h1> php AND myphp or phpme - <u>php!</u>! <img src=\"\" alt=\"great Php\" /><a href='php.com'>php</a> php!   ";
    $word 'php';
    $replace 'asp';
    echo 
    htmlentities(trim(preg_replace("/>([^<]*)(\b" preg_quote($word) . "\b)([^<]*)</ie""'>\\1' . keepcase('\\2', '{$replace}') . '\\3<'"'>' $text '<'), '<>'));  
    die(); 
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    38
    Rep Power
    5
    Thank you!
    I'll try it...

IMN logo majestic logo threadwatch logo seochat tools logo