1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

regular expressions

Discussion in 'PHP & Perl' started by roice, Mar 1, 2010.

  1. roice

    roice Newbie

    Joined:
    Feb 7, 2010
    Messages:
    3
    Likes Received:
    0
    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:
    <a href='http://www.php.com'>myphp</abest <b>php</bwebsite <h1>PhP!!!</h1php and myphp or phpme - <u>php!</u>!
    the result will be
    PHP:
    <a href='http://www.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:
        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.
     
    Last edited: Mar 1, 2010
  2. XoC--

    XoC-- Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 5, 2009
    Messages:
    211
    Likes Received:
    113
    Roi, I think this would do it:

    Code:
    preg_replace("/(>.*?)php(.*?<)/mi", "$1asp$2", $content)
    
    Let me know how it works!
     
  3. roice

    roice Newbie

    Joined:
    Feb 7, 2010
    Messages:
    3
    Likes Received:
    0
    Thanks!
    I'll try that