eoeowner Posted January 29, 2008 Share Posted January 29, 2008 Okay, here's the deal. I have a php based site that shows information about different bands. I want to be able to link to a particular band by clicking on a word within the entered variable for the text. eg: Listen to Tool, they are a top band. - I want the word Tool to be a link to the Tool page. I have worked out by exploding the string, checking each word against the band names and then joining the string back together, I can find any one word key words in any variable.... But, I want to be able to have a phrase. So, I want to be able to find "Iron Maiden" or "Cradle of Filth" and that doesn't work with the join/explode thing since they need a delimiter. Apart from some sort of sadistic SELECT search.... how can I do this??? Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/ Share on other sites More sharing options...
effigy Posted January 29, 2008 Share Posted January 29, 2008 If you're searching through HTML, try this approach. Otherwise, you can simply use a regular expression with word boundaries. Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-452358 Share on other sites More sharing options...
eoeowner Posted January 30, 2008 Author Share Posted January 30, 2008 Okay, I've had a decent go at trying to get it and it's beating me... If i can give you an exact example, can you help me out with the preg_ code? So... $string="I love Iron Maiden they are <b>sooo cool</b>" $keyword="Iron Maiden" $KeywordLinkNumber="34" Ultimately I want to have $string to read... $string="I love <a href="example.php?34">Iron Maiden</a> they are <b>sooo cool</b>" (with slashes if need be) How would you work that? The idea is that the keywords and link numbers are kept in a table and I want to check any posts for links to bands. So that when a post is written ($string) it automatically inserts a link to the band's page. How would I go about doing that? ??? Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-453046 Share on other sites More sharing options...
kenrbnsn Posted January 30, 2008 Share Posted January 30, 2008 You could do something like this: <?php $keywords = array(34 => 'Iron Maiden'); $string = 'I love Iron Maiden they are sooo cool'; foreach ($keywords as $num => $key) $string = str_replace($key,'<a href="example.php?' . $num . '">' . $key . '</a>',$string); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-453047 Share on other sites More sharing options...
eoeowner Posted January 30, 2008 Author Share Posted January 30, 2008 Okay, that seems to be working okay.... A couple of questions, though. Instead of $keywords = array (34 => 'Iron Maiden'); I need to pull a query from the mySQL db how do I translate $keyword and $num into the array ($num => $key) format? Does it change it much? Also, can I make it case insensitive? Thanks for your help btw.... Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-453083 Share on other sites More sharing options...
eoeowner Posted January 30, 2008 Author Share Posted January 30, 2008 Okay I have... <?php $num[1]=34; $num[2]=32; $key[1]="Iron Maiden"; $key[2]="cool"; $keywords = array($num[$a] => $key[$a],$num[2] => $key[2]); $string = 'I love Iron Maiden they are sooo cool and iron maideny'; foreach ($keywords as $num => $key) $string = str_replace($key,'<a href="example.php?' . $num . '">' . $key . '</a>',$string); print $string; ?> ...working for me, but how do I take the next step and automate the population of the $keywords array? Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-453142 Share on other sites More sharing options...
kenrbnsn Posted January 30, 2008 Share Posted January 30, 2008 You don't need the extra arrays. Change your example to: <?php $keywords = array(34 => "Iron Maiden",32 => 'cool'); $string = 'I love Iron Maiden they are sooo cool and iron maideny'; foreach ($keywords as $num => $key) $string = str_replace($key,'<a href="example.php?' . $num . '">' . $key . '</a>',$string); print $string; ?> If you need to automagically add to this array using a database. do something like: <?php // // your mysql query here // while ($rw = mysql_fetch_assoc($result)) $keywords[$rw['num']] = $rw['keyword']; ?> Or just do the replace in the database while loop: <?php // // your mysql query here // while ($rw = mysql_fetch_assoc($result)) $string = str_replace($rw['keyword'],'<a href="example.php?' . $rw['num] . '">' . $key . '</a>',$string); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-453282 Share on other sites More sharing options...
eoeowner Posted January 31, 2008 Author Share Posted January 31, 2008 Okay, I have tried as you suggested, inserted the mySQL commands and change the variable names to match. Worked perfectly. Thanks to all who helped. You guys are awesome. Quote Link to comment https://forums.phpfreaks.com/topic/88385-solved-searching-for-key-words-in-a-string/#findComment-454751 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.