str_replace, replacing words

Discussion in 'PHP' started by xtreme fever, Dec 5, 2008.

  1. #1
    i have a web site where i started to replace words. examples, search string (q) = ipod, web page title = iPod. search string (q) = apple iphone, web page title = Apple iPhone etc... now i've been using str_replace...

    $a = "$q";
    $b = array("ipod","apple iphone");
    $c = array("iPod","Apple iPhone");
    $q = str_replace ($b, $c, $a);

    ... to replace the text. the pages are dynamic and titles, meta, h1 etc... are created depending on the keyword searched.

    since there would be a lot of words to add to the arrays, is there a better way i can handle this replacement? right now i'm adding all the str_replace words in one file and soon that file is going to become big.

    i'm not sure if i have (or is) a mysql option that will allow me to do the same, replacing the text.
     
    xtreme fever, Dec 5, 2008 IP
  2. MaradoX

    MaradoX Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    store all words in a table or so.
    For example:
    table words:
    One field: id auto increment
    second field: word varchar (the original word which should be replaced)
    third field: diff_word varchar (the new word)

    then do a query and get all those words in an array. This should be easy.
    $sql="SELECT word, diff_word FROM words ORDER BY id DESC"; (this ordering is optional, is not necessary).
    $result=mysql_query($sql) or die(mysql_error());
    while($row=mysql_fetch_array($result)){
    
    $q=str_replace($row['word'],$row['diff_word'],$q);
    
    }
    PHP:
    this should replace all those words.
    Although I have to say this is just very basic code.
    it just gives an idea
     
    MaradoX, Dec 6, 2008 IP
  3. Muism4t

    Muism4t Active Member

    Messages:
    114
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    with maradox.

    You´re more flexible if you´re using a SQL table. You can even add things like this, to easily define which of the replacement rules should be applied.

    fourth field: visible integer (should this replacement rule be applied?)

    The query would then be "SELECT word, diff_word FROM words WHERE visible=´1´ ORDER BY id DESC"
     
    Muism4t, Dec 6, 2008 IP
  4. xtreme fever

    xtreme fever Active Member

    Messages:
    472
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #4
    thanks guys
     
    xtreme fever, Dec 8, 2008 IP
  5. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    One more note. If you are looking to squeeze performance out of your script, use strtr instead of str_replace. Check out php.net/strtr.
     
    xxKillswitch, Dec 8, 2008 IP
  6. xtreme fever

    xtreme fever Active Member

    Messages:
    472
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #6
    i am coming across some problems with the str_replace method. is there another way i can do this but, find and replace exact keyword terms? i can use; if ($q=="")$q=""; but, this file too will become large.
     
    xtreme fever, Dec 15, 2008 IP
  7. xxKillswitch

    xxKillswitch Peon

    Messages:
    331
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Even doing this by database, sounds like you would have alot of manual work. You can try setting an array of keywords and setting their value to the associated word, for example...

    $array = array( 'iphone' => 'iPhone', 'whatever' => 'Whatever' );

    And do the replacement this way. You can use in_array() to find if the keyword is in the array and use the value of that array key instead of doing any replacement. That would honestly probably be faster than doing any replacements, though you might end up with quite a large array.

    Last question, is there any specific reason you need to have these capitalized the way you do to make all this work worth it?
     
    xxKillswitch, Dec 15, 2008 IP
  8. xtreme fever

    xtreme fever Active Member

    Messages:
    472
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #8
    thanks. i know it will be a lot of manual work but, it's worth it. i don't want to have the search query return in lowercase and having iPhone or iPod looks better in titles and headers than iphone or ipod. and though i have to use a system for my .com and .co.uk domains, i figured i could use a database for both and for more if i expand to other domains.
     
    xtreme fever, Dec 16, 2008 IP