matching words

Discussion in 'PHP' started by roice, Feb 25, 2010.

  1. #1
    Hello,
    I'm looking for PHP function that do matching between specific word ($search_word) and all the words in my html file ($content = strtolower(file_get_contents($folder.$file));)
    if the function find matching it replace the word in the html text with another word. The problem is with capital letters - there won't be any matching if the words in "$content" will begin with capital letters (or will be all with big letters).
    Is there any function that do matching while it ignore capital letters?
     
    roice, Feb 25, 2010 IP
  2. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #2
    To search and replace i would use str_ireplace() this function is case-insensitive
     
    n3r0x, Feb 25, 2010 IP
  3. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks n3r0x!
     
    roice, Feb 25, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    You can use preg_replace with the 'i' modifier.

    <?php
    
    $content = "Hello, im testing with caps; Hello!";
    
    $search_word = "hello";
    
    $another_word = "bye";
    
    echo preg_replace("/\b".$search_word."\b/i", $another_word, $content);
    
    ?>
    PHP:
     
    danx10, Feb 25, 2010 IP
  5. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks danx10!
    One more thing - I have function that run over string that includes HTML tags and replace the word "php" (as it is) with "asp". if the text "myphp" will be found, the script won't replace it to myphp.
    The problem is with the HTML tags - I need to replace only the texts between the HTML tags. for example - if the string is:
    <a href='http://www.php.com'>myphp</a> best <b>php</b> website <h1>PhP!!!</h1> php and myphp or phpme - <u>php!</u>!
    PHP:
    The result will be:
    <a href='http://www.php.com'>myphp</a> best <b>asp</b> website <h1>asp!!!</h1> aspand myphp or phpme - <u>asp!</u>!
    PHP:
    The script that I already have is:
    
    	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; 	
    PHP:

    What should I change if I want the text between the HTML tags to be replaced?
     
    roice, Feb 28, 2010 IP
  6. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm pretty sure that its something to do with the preg function, but I'm not sure what...
     
    roice, Mar 1, 2010 IP
  7. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #7
    Hi,try this code:

    
    echo preg_replace('#(\>.*?)\b(php)\b(.*?\<)#misux','$1 asp $3',$string)."\n";
    
    PHP:
    Regards :)
     
    koko5, Mar 1, 2010 IP
  8. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi koko5,
    Thanks for your help!

    The input was:
    <a href='http://www.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" />
    PHP:
    wnd the output is:
    <a href='http://www. asp .com'>myphp</a> best <b> asp </b> website <h1> asp !!!</h1>  asp  and myphp or phpme - <u> asp !</u>! <img src="" alt="great  asp " />
    PHP:
    so it didn't work...

    This is the my script:
    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;
    PHP:
    I changed the line before the last one into the code you gave me, without any success...:/
     
    roice, Mar 1, 2010 IP
  9. redlightshooter

    redlightshooter Greenhorn

    Messages:
    94
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #9
    i have same problem, it's about regular expression..., does anybody have a referencies to learn deeper about it??
     
    redlightshooter, Mar 1, 2010 IP
  10. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #10
    Why don't you simply do this?
    $output=str_ireplace(" $search_word ", $another_word, $output);
    PHP:
    It'd be much cleaner and efficient to use ''.$search_word.'' instead of " $search_word ", but this way you can clearly see what I meant.

    Hope it helps!
     
    HostingProvider, Mar 1, 2010 IP
  11. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi HostingProvider,
    It won't work -
    a. I want to replace the exact work -"php" (no matter if there are capital letters) with the word "asp", so the word "myphp", for example, won't be replace. only clear "php".
    b. I don't want that text inside the HTML tags will be change. only the text between the HTML tags. for example - <a href='http://www.php.com'>php</a> - only the "php" between the <a> and </a> need to be change.
    your function alone can't do that...:/
     
    roice, Mar 1, 2010 IP
  12. roice

    roice Peon

    Messages:
    200
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Is there anyone here that can help me with this regular expressions?
     
    roice, Mar 2, 2010 IP