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

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6

    Help with str_replace using an array


    Right now I have a function setup and I call it on the page that it needs to be stripped before entering the database. However, it doesn't work.

    Here is the function:

    Code:
    function generate_seo_link($scampost, $replace = '[CENSORED]', $words_array = array()) {
    
      //remove words, if not helpful to seo
      //i like my defaults list in remove_words(), so I wont pass that array
      $scampost = str_replace($words_array, $replace, $scampost); 
    
      return $scampost;
    }
    Here is where I all the function:

    Code:
    $query = "select * from bad_words";
    $result = mysql_query($query);
    
    
    while ($record = mysql_fetch_assoc($result)) 
    {
    $words_array[] = $record['word'];
    }
    
    require_once "../functions/functions.php";
    $scampost = generate_seo_link($scampost, true, $words_array);
    What in the world is wrong with this. I am just now learning functions and arrays. It only replaces the bad words in the beginning (not all) but with the number "1" and not the word "[CENSORED]".

    For example if the word "bad" should be replaced here is my string:

    bad bad This should be the only remaining text. bad

    Should output: This should be the only remaining text.

    However, it outputs: 11 This should be the only remaining text. bad

    What in the world is wrong with this? Thanks for any help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    102
    Rep Power
    10
    I might be wrong, but by sending true to the function, aren't you setting $replace as a boolean? That would be where the 1s are coming from.

    In your case, I wouldn't use default values in the function like you have. I would do something like this:

    PHP Code:
    function generate_seo_link($scampost$replace$words_array){
        if(
    $replace == ""){
            
    $replace '[CENSORED]';
        }
        if(!
    is_array($words_array){
            
    $words_array = Array();
        }
        
        
    //remove words, if not helpful to seo
        //i like my defaults list in remove_words(), so I wont pass that array
        
    $scampost str_replace($words_array$replace$scampost); 
        
        return 
    $scampost;

    Then, each time you call the function you can either specify values for $replace and $words_array, or pass empty strings like this:

    PHP Code:
    $scampost generate_seo_link($scampost''''); 
    I hope that helps.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Thank you for your reply. I just tried this but I am getting some sort of error b/c the form is not being submitted after changing the code to this.

    I use VPS Hosting so I am having to contact them to figure out how to turn on error reporting so I can see what error is being output.

    Any idea right off what it might be?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Ok, I was able to get it to work with the following code. However, if the word being replaced had a space after it it looses that space. For some reason if it had a space before it it does not loose that space. Any idea how to fix that?

    Code:
    function generate_seo_link($scampost, $replace, $badwords = array())
    {
        
        //remove bad words
        $scampost = str_replace($badwords, $replace, $scampost); 
        
        return $scampost;
    }
    Code:
    $query = "select * from bad_words";
    $result = mysql_query($query);
    
    
            while ($record = mysql_fetch_assoc($result)) 
        {
            $badwords[] = $record['word'];
        }
    
    $replace = "[CENSORED]";
    require_once "../functions/functions.php";
    $scampost2 = generate_seo_link($scampost, $replace, $badwords);

    Now image if the word "dog" was a bad word that needs replaced.
    Code:
    $scampost = "dog The previous word should have been replaced. dog";
    Here is the output.

    [CENSORED]The previous word should have been replaced. [CENSORED]

    Notice the missing space.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Do you have spaces in your database? The words in the table probably have spaces after them.

    Comments on this post

    • tech0925 agrees
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Originally Posted by ManiacDan
    Do you have spaces in your database? The words in the table probably have spaces after them.
    The database for the bad words is setup as varchar 255 characters. There is only one word inserted per row. I clicked on inline edit in phpmyadmin for a couple of words to be thorough and did notice it contains a space. So is this causing the issue?

    Let me try to remove the spaces and see what happens. I'll let you know.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Awesome, that fixed it! Thanks! I added you some rep

    One more question. In my database some of the bad words are capitalized and some are not. If the bad word is not exactly like it is in the database and array it does not censor it? For example: In the case the words "Bad Dog" are bad words that should be censored. However, if the string contains "bad dog" instead it does not censor it.

    How can I fix this regardless of whether or not it is capitalized or not? What should I add to the function that will correct that?

    Thanks 4 the help!
  14. #8
  15. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Originally Posted by tech0925
    How can I fix this regardless of whether or not it is capitalized or not? What should I add to the function that will correct that?
    Use str_ireplace() instead of str_replace.

IMN logo majestic logo threadwatch logo seochat tools logo