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

Str Replace multiple words once

Discussion in 'PHP & Perl' started by jimbbob, Oct 2, 2012.

  1. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    Okay so I want to replace 2 words with the same words in a different format but only once per occurance.

    PHP:
    <?php
    $text 
    "Word 1";
    $newtext str_replace(array('Word 1','Word 2'),'Word 1,Word 2',$text);
    echo 
    $newtext "<br>\n";
    ?>
    This outputs:
    Word 1,Word 1,Word 2

    I want it to instead out put like this:
    Word 1,Word2
     
    Last edited: Oct 2, 2012
  2. mrankin

    mrankin Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 17, 2008
    Messages:
    1,215
    Likes Received:
    571
    Location:
    Australia
    Home Page:
    I'd use a preg_match on the array instead of str_replace. That way you can store whichever matches you want into variables.
     
  3. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    525
    Likes Received:
    311
    Gender:
    Male
    I think you are confused about the str_replace usage:

    Code:
    $a = array( 'truck', 'vehicle', 'sedan', 'coupe' );
    $str = 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.';
    echo str_replace($a,'car',$str);
    Will produce the outpuit:
    Code:
    Honda is a car. Toyota is a car. Nissan is a car. Scion is a car.
    The usage is:
    Code:
    [I]str_replace(search, replace, string)[/I]
    And from what I understood from your example, this is what your code is currently doing is:

    1) Search for Word 1
    2) Found Word 1
    3) Change Word 1 into Word 1,Word 2
    4) Search for Word 2
    5) Found Word 2 from item 3 changes
    6) Change Word 2 into Word 1,Word 2
    7) Result is Word 1,Word 1,Word 2
     
    Last edited: Oct 2, 2012
  4. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    lol its funny, the code you gave me is the same one i posted on other websites.

    The problem is that when I replace that car with its complete list of synonyms like this:

    PHP:
    <?php
    $a 
    = array( 'truck''vehicle''sedan''coupe' );
    $str 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.';
    echo 
    str_replace($a,'car,coupe,sedan',$str);
    ?>

    Then the result ends up being:
    Honda is a car,car,coupe,sedan,car,car,coupe,sedan,sedan. Toyota is a car,car,coupe,sedan,car,car,coupe,sedan,sedan. Nissan is a car,car,coupe,sedan,sedan. Scion is a car,coupe,sedan.

    I'm trying to get it to out put as

    Honda is a car,coupe,sedan. Toyota is a car,coupe,sedan.... and so on.

    What the script does is it replaces the 1st time it sees "car" and replaces it with "car,coupe,sedan" then it sees that this "new replacement" also has the word "coupe" so it replaces that with "car,coupe,sedan" and so on. I want it to only replace the synonym once everytime it appears. Thanks.
     
  5. cgimaster

    cgimaster Power Member

    Joined:
    Jun 30, 2012
    Messages:
    525
    Likes Received:
    311
    Gender:
    Male
    Like I explained above, that happens because you are using the same terms on the replace and search.

    So when it replaces it into "car,coupe,sedan" on the next item from your search array, it will replace everything again.

    Think it like this, it replace your text with the first array, now your text is not longer:

    Code:
    $str = 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.';
    But rather it became:

    Code:
    $str = 'Honda is a car,coupe,sedan. Toyota is a car,coupe,sedan. Nissan is a car,coupe,sedan. Scion is a car,coupe,sedan.';
    Then when it gets into the 2 array item it finds "coupe and sedan" and it will replace the 2 with the terms you have set to replace with.

    So basicaly youre entering a loop.
     
  6. Zak_A

    Zak_A Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2008
    Messages:
    808
    Likes Received:
    873
    Gender:
    Male
    Occupation:
    WP designer & developer
    Location:
    Western Europe
    I think I understand what you're trying to do.
    If I get it correctly the best way would be to split the job in two parts.

    Part 1: find all the words you want to replace and replace them with a temporary token.
    Part 2: replace the token with whatever you want.

    Exemple :

    PHP:
    <?php
    $a 
    = array( 'truck''vehicle''sedan''coupe' );
    $str 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.';
    $tmp str_replace($a,'###',$str);
    echo 
    str_replace('###','car,coupe,sedan',$tmp);
    ?>
    What I'm doing here is first replace 'truck', 'vehicle', 'sedan' and 'coupe' with '###'.
    So my $tmp variable now contains "Honda is a ###. Toyota is a ###. Nissan is a ###. Scion is a ###."
    Then I replace ### with what I want, thus avoiding a loop :)

    I guess you're trying to create a spintax with this script am I right ? ;)
     
  7. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    Yes, that is the problem. Do you know how to have it output correctly? Like is there some sort of a limit you can put in the code
     
  8. Zak_A

    Zak_A Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2008
    Messages:
    808
    Likes Received:
    873
    Gender:
    Male
    Occupation:
    WP designer & developer
    Location:
    Western Europe
    Isn't my solution what you were looking for ?
     
  9. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    WOW!! Thats an amazing trick. You're a genius. That's exactly what I've been trying to do. Thank you so much. Really! That was an amazing trick to get out of the loop! Thanks
     
    Last edited: Oct 2, 2012
  10. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    Yes exactly what I was looking for.. Im trying to edit the code and I'm wondering if you can show me how I can use multiple str_replaces.. For example if i also wanted to include a second str_replace that did the exact same thing for Honda Toyota. So that it looks like

    Honda,Toyota,Nissan,Scion is a car,coupe,sedan. Honda,Toyota,Nissan,Scion is a car,coupe,sedan. Honda,Toyota,Nissan,Scion is a car,coupe,sedan. Honda,Toyota,Nissan,Scion is a car,coupe,sedan.
     
  11. Zak_A

    Zak_A Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2008
    Messages:
    808
    Likes Received:
    873
    Gender:
    Male
    Occupation:
    WP designer & developer
    Location:
    Western Europe
    Simply do it twice with different temp tokens, such as "##1" and "##2".

    PHP:
    <?php

    $array1 
    = array( 'truck''vehicle''sedan''coupe' );
    $replace1 "car,coupe,sedan";

    $array2 = array('word1, word2, word3');
    $replace2 "word1,word2,word3";

    $text 'Your text goes here';

    $tmp str_replace($array1,'##1',$text);
    $tmp str_replace($array2,'##2',$tmp);

    $result str_replace('##1',$replace1,$tmp);
    $result str_replace('##2',$replace2,$result);

    echo 
    $result;

    ?>
    If you want to add a many bunches of words to replace, you'll need to use arrays and foreach loops, but here you have the basic idea of how to make it work.
     
  12. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    Yea I probably need to create 100s of arrays for what I'm trying to do. I'm trying to create an automated article spinning script that recognizes common english words and throws in its synonyms so that i can use an article spinner to create different articles from the same text. Thank you very much for your help. I understand how to use it.

    PS: Is it possible to find words with/without case sensitivity. So that it replaces Toyota and toyota?
     
    Last edited: Oct 2, 2012
  13. Zak_A

    Zak_A Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2008
    Messages:
    808
    Likes Received:
    873
    Gender:
    Male
    Occupation:
    WP designer & developer
    Location:
    Western Europe
    str_replace() is case sensitive. You can also use str_ireplace() which is the case insensitive version.
     
  14. anhkiet

    anhkiet Newbie

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    11
    Home Page:
    You are running into a pretty classic problem where the item you are looping over is changing. In this case, str_replace is being smart about it and you don't loop forever, but you still run into the problem of having substrings being replaced multiple times.

    If you take Zak_A's solution and turn it into a function, then you can do this with multiple str_replaces. Essentially you have a wrapper function around str_replace to do some preprocessing:

    PHP:
    function token_replace($search$replace$subject) {
        
    $placeholder '%%placeholder%%';
        
    $tmp str_replace($search$placeholder$subject);
        return 
    str_replace($placeholder$replace$tmp);
    }
    This is how you would use it:
    PHP:
    $text 'Honda is a truck. Toyota is a vehicle. Nissan is a sedan. Scion is a coupe.';
    $arr = array( 'truck''vehicle''sedan''coupe' );
    $replace "car,coupe,sedan";
    $text token_replace($arr$replace$text);

    $arr = array('Honda''Toyota''Nissan''Scion');
    $replace implode(','$arr); // Creating the string 'Honda,Toyota,Nissan,Scion'
    $text token_replace($arr$replace$text);
    echo 
    $text;
    The output text:
    Code:
    Honda,Toyota,Nissan,Scion is a car,coupe,sedan. Honda,Toyota,Nissan,Scion is a car,coupe,sedan. Honda,Toyota,Nissan,Scion is a car,coupe,sedan. Honda,Toyota,Nissan,Scion is a car,coupe,sedan.
    
    Assuming good implementation for str_replace, the runtime complexity of this code is O(m * n) where m is the number of items to be replaced and n is the length of the string. Probably insignificant for the amount of text processing that you do.

    By the way, does anyone else think that the syntax highlighting for PHP is hideous? It's horrible on the eyes and makes read the code nearly impossible.
     
  15. anhkiet

    anhkiet Newbie

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    11
    Home Page:
    Another thing to note that I changed the place holder string from ### to %%placeholder%%. The second is more unique and reduces the chances of having a collision with something well known. For example, markdown uses ### to indicate an H3.
     
  16. jimbbob

    jimbbob Newbie

    Joined:
    Oct 2, 2012
    Messages:
    10
    Likes Received:
    0
    Thank you anhkiet for the suggestions and Zak_A for the code. I'm implementing it now with my actual uses. Thanks again!