Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regular expressions

    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='http://www.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='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 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
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    $result preg_replace('/(>[^<]*)\b(' preg_quote($word) . ')\b/sime'"$1.keepcase('$2', '$replace')"$subject); 
    Something like that will do it
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi JAY6390,
    Thank you for your reply!
    here is the code with your changes:

    PHP Code:
        function keepcase($word$replace) {
          
    $replace[0] = (ctype_upper($word[0]) ? strtoupper($replace[0]) : $replace[0]);
          
          return 
    $replace;
        }

        
    $text file_get_contents($folder.$file);
        
    $replace "asp";
        
    $word "php";
        
    $output preg_replace('/(>[^<]*)\b(' preg_quote($word) . ')\b/sime'"$1.keepcase('$2', '$replace')"$text);  
        echo 
    $output
    But I got an error message:

    Parse error: syntax error, unexpected '>' in /home/scrambler/scrabler.php(66) : regexp code on line 1

    Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: &gt;.keepcase('php', 'asp')


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •