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.
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
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"
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.
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.
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?
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.