dflow Posted May 16, 2011 Share Posted May 16, 2011 how should i approach this with preg_match? i have a text: "big red apple" now i want the script to create a href for each keyword apple and link to http://apple.com examples appreciated Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/ Share on other sites More sharing options...
dragon_sa Posted May 16, 2011 Share Posted May 16, 2011 <?php $text="big red apple at microsoft"; $change=array("apple", "microsoft"); $with=array("<a href='http://www.apple.com'>apple</a>", "<a href='http://www.microsft.com'>microsoft</a>"); $changed=str_replace($change, $with, $text); echo "Original text was $text <br/>"; echo "New text is $changed"; ?> Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/#findComment-1216014 Share on other sites More sharing options...
dflow Posted May 16, 2011 Author Share Posted May 16, 2011 <?php $text="big red apple at microsoft"; $change=array("apple", "microsoft"); $with=array("<a href='http://www.apple.com'>apple</a>", "<a href='http://www.microsft.com'>microsoft</a>"); $changed=str_replace($change, $with, $text); echo "Original text was $text <br/>"; echo "New text is $changed"; ?> mucho grasias!!! Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/#findComment-1216024 Share on other sites More sharing options...
PFMaBiSmAd Posted May 16, 2011 Share Posted May 16, 2011 If you use preg_replace, you can do a case-insensitive search and you can get the code to form the links for you - <?php $search = "apple"; // use the | to separate multiple keywords - "red|apple" $string = "big red apple"; $string = preg_replace("/($search)/i",'<a href="http://\1.com">\1</a>',$string); echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/#findComment-1216032 Share on other sites More sharing options...
dragon_sa Posted May 16, 2011 Share Posted May 16, 2011 does that also create multiple different links aswell? eg apple.com and red.com if both keywords were entered or is the syntax different Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/#findComment-1216036 Share on other sites More sharing options...
PFMaBiSmAd Posted May 16, 2011 Share Posted May 16, 2011 <?php $search = "proposition|seven|nation|liberty|continent"; // use "red|apple" to match multiple keywords $string = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal."; $string = preg_replace("/($search)/i",'<a href="http://\1.com">\1</a>',$string); echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/#findComment-1216039 Share on other sites More sharing options...
dragon_sa Posted May 16, 2011 Share Posted May 16, 2011 very nifty Link to comment https://forums.phpfreaks.com/topic/236539-intext-linking-specific-phrases-keywords/#findComment-1216041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.