Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Turning keywords from text into links

Status
Not open for further replies.

hallm

Programmer
Jun 11, 2000
159
US
I've got a script that I use to update pages on my website. I've got three variables that apply to my issue.

$keywords Keywords that I designate
$link Link where I want the visitors to go
$text The actual text thats displayed

What I'm wanting to do is somehow parse the text before it is echoed and make a like everytime the keywords are used, then display. Example below.

$keywords = 'chair';
$link = '$text = 'This is sample text about a chair. A chair is';

When $text is echoed, I'm wanting the two instances of chair to be replaced with '<a href="
I'm not wanting anyone to write any code for me, I'm just stumped as to which functions to look at to achieve my goal.

Thanks for your help.

Marion

hallm
 
You can use PCRE (Perl Compatible egular Expressions) or simple str_replace() functions.

The challenge is not as easy as it sounds since there may be many complicating factors when inserting HTML markup into existing text.

If you just want to replace let's say all occurrences of the word 'chair' str_replace('chair','<a href=" in a plain text file you'll be fine.

However, if there are words that are part matches, let's make up a person named Schaira McKenzie the 'chair' part would also be replaced.
What if the word chair apperas within an HTML tag, like an image tag <img="chair.jpg"> ? A substitution would mess up the HTML.

PCRE are much more powerful but at first difficult to understand. Look at them, anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top