Jump to content

Recommended Posts

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!

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.

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

?>

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.