NeoMarine Posted February 2, 2008 Share Posted February 2, 2008 Hi, I was wondering if anyone knows a method (or if it is possible) to, using PHP make a word a link? For example, I have on my page the following text: Adam went to the supermarket to find apples, but there was only bananas! I feel sorry for Adam, don't you? I want to change that to: <a href="link.php">Adam</a> went to the supermarket to find apples, but there was only bananas! I feel sorry for <a href="link.php">Adam</a> , don't you? Before it hits the page, because it contains the words Adam. Now, the text is already on the page in HTML format mixed with the PHP... so is there a way to grab the HTML off the page as its being created without having to echo the html and add the links then? Understand my query? Please help! Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/ Share on other sites More sharing options...
NeoMarine Posted February 2, 2008 Author Share Posted February 2, 2008 To be more clear, an example of what I want would be a function that searches all the html/page for the occurances of a word such as "adam" and then replacing it with "<a href="link.php">adam</a>"... My only other solution is to, after all my pages are complete, go through and do a manual replace-all but I wanted to have it work at runtime so I can change things faster later on. Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-455820 Share on other sites More sharing options...
laffin Posted February 2, 2008 Share Posted February 2, 2008 <?php $mywordlinks = array( 'adam' => 'http://www.adam.com', 'eve' => 'http://www.eve.com', 'apples' => 'http://www.apple.com' ); function ismyword($matches) { global $mywordlinks; $word=$matches[1]; if(isset($mywordlinks[$key=strtolower($word)])) { $word="<A HREF='$mywordlinks[$key]'>$word</A>"; } return $word; } $string="Adam went to the supermarket to find apples, but there was only bananas! I feel sorry for Adam, don't you?"; $newstring=preg_replace_callback("@(?=[ ]?)([a-z\']{3,})(?=[\s\.,?!])@im",'ismyword',$string); echo "<PRE>$string\n$newstring</PRE>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-455832 Share on other sites More sharing options...
resago Posted February 2, 2008 Share Posted February 2, 2008 move your html into a big string $html then preg_replace('/ adam /is',"<a href='whatever'>adam<\/a>",$html); Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-455847 Share on other sites More sharing options...
resago Posted February 2, 2008 Share Posted February 2, 2008 if you want this to act on ANY html page on your site, you could use a ReWrite rule and run requests through your script. Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-455850 Share on other sites More sharing options...
NeoMarine Posted February 2, 2008 Author Share Posted February 2, 2008 Is it possible to use a rewrite rule within the PHP for this purpose? I want to do the changes during PHP but not within the page if you know what I mean... just before the page is loaded to the user, all the links should be replaced into the appropriate words... I have no clue about how to use rewrite for purposes OTHER than for the URL? I use Isapi Ionic Rewriter for this, when required... Maybe you can provide an example for me? Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-455861 Share on other sites More sharing options...
resago Posted February 2, 2008 Share Posted February 2, 2008 something like ReWriteRule (.+\.html) filter.php?page=$1 then your filter.php $html=file_get_contents($_GET['page']); preg_replace('/ adam /is',"<a href='whatever'>adam<\/a>",$html); echo $html; Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-456146 Share on other sites More sharing options...
resago Posted February 2, 2008 Share Posted February 2, 2008 oopps $html=file_get_contents($_GET['page']); $html=preg_replace('/ adam /is',"<a href='whatever'>adam<\/a>",$html); echo $html; Quote Link to comment https://forums.phpfreaks.com/topic/89007-generating-links-with-php-based-on-specific-words/#findComment-456153 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.