surfwtw Posted March 2, 2012 Share Posted March 2, 2012 I have a website that has definitions and articles. I need a code that craws the php content pulled from my database, finds a specified word and replaced that word with a link. For example If "car" appears replace with <a href="http://www.website.com/car.php">car</a> Thanks Todd Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/ Share on other sites More sharing options...
darkfreaks Posted March 2, 2012 Share Posted March 2, 2012 why not just replace 'cars' with str_replace('car','<a href="http://www.website.com/car.php">car</a>',$query); ' Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323287 Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2012 Share Posted March 2, 2012 Then what happens to "carnival" or "scary"? Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323298 Share on other sites More sharing options...
surfwtw Posted March 2, 2012 Author Share Posted March 2, 2012 Well my goal is to have it crawl the content on the display page and if the word is there it will turn it into a link. I have over 1000 definitions and I want there to be links to the url whenever one of the words appears. This means that I would probably have to list a code for every word that I have content for but that would be better than physically reading and locating the words and then adding links. The display page is only one page that displays many different pages of data so say I clicked on the definition for "trailer" and the word "car" appeared then I would want to be able to click on "car" and have a new page open up with the definition of "car". Obviously I would have to repeat the code for every word I wanted to be linked. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323299 Share on other sites More sharing options...
surfwtw Posted March 2, 2012 Author Share Posted March 2, 2012 Yeah that could be a problem but luckily my website is a finance website so the words are complex. BTW I plugged that code in and it didn't do anything. Not sure why. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323300 Share on other sites More sharing options...
Anon-e-mouse Posted March 2, 2012 Share Posted March 2, 2012 Obviously I would have to repeat the code for every word I wanted to be linked. Hardly, If you stuck the words you had in an array for example and foreach'ed through them, if a match was found then you could query a table for the definition and stick the replace script in there. In fact if you were to just query the table with your words and definitions in you would save doing the above as you could loop through the results and use the original query to power the string replace in the event of a match! That saves you.. for 1000+ definitions, a lot of copy and pasting. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323301 Share on other sites More sharing options...
surfwtw Posted March 2, 2012 Author Share Posted March 2, 2012 I have the following code which automatically bolds the "word" that is being defined. Is there a way that I could manipulate this code to make it link words other than the "word"? function boldWord($word,$definition){ $word = strtolower($word); $ucf_word = ucfirst($word); $uc_word = strtoupper($word); return str_replace(array($word,$ucf_word,$uc_word), array("<b>$word</b>","<b>$ucf_word</b>","<b>$uc_word</b>"), $definition); } Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323305 Share on other sites More sharing options...
surfwtw Posted March 2, 2012 Author Share Posted March 2, 2012 Anon-e-mouse Do you have an example that I could play around with. I am familiar with the PHP concept but not fluent with the coding. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323307 Share on other sites More sharing options...
freelance84 Posted March 2, 2012 Share Posted March 2, 2012 Will the url in the link always be the same? Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323309 Share on other sites More sharing options...
Anon-e-mouse Posted March 2, 2012 Share Posted March 2, 2012 Anon-e-mouse Do you have an example that I could play around with. I am familiar with the PHP concept but not fluent with the coding. Sure, <?php $sql = "SELECT * FROM ....."; $run = mysql_query($sql); while($get = mysql_fetch_object($run)){ if($wordyouarelookingat == $get->word){ function($wordyouarelooking at); } else { continue; } } ?> There you have a really basic example of what I was going on about. Simply you would query the table in order to get your list of words, run through the results and if the word you are searching for is the same as one in the table then you can pass it through the function otherwise it would skip that word and try the next..? Obviously I don't know how you will get the words to highlight in the first place but the above is just one way out of a possible trillion (most likely...) to attempt it. Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323313 Share on other sites More sharing options...
surfwtw Posted March 2, 2012 Author Share Posted March 2, 2012 What does $run do? Am I supposed to change that to something or do I leave it the same? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323318 Share on other sites More sharing options...
darkfreaks Posted March 2, 2012 Share Posted March 2, 2012 could do something like this $replacement_words = array('cars'=>'<ahref="mysite.com/cars.php">cars</a>'); return str_replace(array_keys($replacements),$replacements,$word) you can play around with this example. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323320 Share on other sites More sharing options...
Anon-e-mouse Posted March 3, 2012 Share Posted March 3, 2012 What does $run do? Am I supposed to change that to something or do I leave it the same? Thanks Morning (just), $run if you look is the query actually being executed instead of putting the query inside mysql_query() directly I built the syntax and then input it. So $run becomes the "query" in the sense that whenever you wanted to pull the results you would use that. Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323328 Share on other sites More sharing options...
surfwtw Posted March 3, 2012 Author Share Posted March 3, 2012 OK I am definitely having problems and I appreciate all the help. I am already connected with the code below <?php // Use the URL 'word' variable to set who we want to query info about $word = $_GET['word']; // filter everything but numbers for security if ($word == "") { echo "Missing Data to Run"; exit(); } //Connect to the database through our include include_once "connect_to_mysql.php"; // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM dictionary WHERE word='$word' LIMIT 1"); $count = mysql_num_rows($sql); if ($count > 1) { echo "There is no user with that id here."; exit(); } while($row = mysql_fetch_array($sql)){ $word = $row["word"]; $definition = $row["definition"]; $equation = $row["equation"]; $example = $row["example"]; $keywords = $row["keywords"]; $related_words = $row["related_words"]; $name = $row["name"]; $email_address = $row["email_address"]; $site = $row["site"]; } ?> And below is the code that I use to pull out the text to display. I have a code that automatically bolds the "word that is being defined" but I also want to add a code that finds certain words and links them to their definitions. <?php function boldWord($word,$definition){ $word = strtolower($word); $ucf_word = ucfirst($word); $uc_word = strtoupper($word); return str_replace(array($word,$ucf_word,$uc_word), array("<b>$word</b>","<b>$ucf_word</b>","<b>$uc_word</b>"), $definition); } $text = "$definition"; echo boldWord("$word",$definition);?> Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323338 Share on other sites More sharing options...
darkfreaks Posted March 3, 2012 Share Posted March 3, 2012 cleaned up your IF's with ternary operators. <?php // Use the URL 'word' variable to set who we want to query info about $word = $_GET['word']; // filter everything but numbers for security //using ternary operator to say if empty or equals empty to error else display word. echo ($word==''||empty($word)) ? "Missing Data to Run" : $word; exit(); } //Connect to the database through our include include_once "connect_to_mysql.php"; // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM dictionary WHERE word='$word' LIMIT 1"); $count = mysql_num_rows($sql); //ternary if count greater than one error else $count echo ($count > 1) ? "there is no user with that ID" : $count; } while($row = mysql_fetch_array($sql)){ $word = $row["word"]; $definition = $row["definition"]; $equation = $row["equation"]; $example = $row["example"]; $keywords = $row["keywords"]; $related_words = $row["related_words"]; $name = $row["name"]; $email_address = $row["email_address"]; $site = $row["site"]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258145-automatic-linking-code-if-car-appears-replace-with/#findComment-1323359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.