CoderZone.org

Category: >> PHP Code >> Auto-link text with a given set of Bookmark and Share

<< lastnext >>

Snippet Name: Auto-link text with a given set of

Description: This bit of code turns words from a list into links with a URL target taken from the "reserved word list". When using both singular and plural match words, make sure the plurals come before the singular version (i.e. 'dogs' and 'dog').

Also see:
» Countdown to Enable a Download Link
» Find Symlink Targets
» Search and Replace On Every Field ...
» Regular Expressions - REGEXP_REPLA...
» Database Links: Close Database Link
» Database Links: CURRENT_USER
» Create Database Link
» Database Links
» Inline search and replace
» Extract and Display all links on a...
» NVL: Replace NULL values
» Column copy with string replacement
» Replace a NULL with a given value.
» DBA Links
» Database Link
» Truncate Links Over X Chars long
» Search and Replace
» Nice 'Read More...' links
» Get Link Text
» Auto Link Maker #2
» Timed link enabler
» Auto-Link words
» Attach to all links
» Show all links
» Create DB Link
» Secure Auto-Link Maker
» Auto Link Code

Comment: (none)

Author: Wheels
Language: PHP
Highlight Mode: PHP
Last Modified: February 26th, 2011

// list of keywords to auto-link 
// list plural forms first
$reserved_word_list = array	(
 
	'dogs' => 'http://dogs.com', 
	'dog' => 'http://dogs.com', 
	'cat' => 'http://cats.com', 
	'kitten' => 'http://cats.com',
	'horse' => 'http://horses.com'
}
 
 
// search text string and auto-link the words
foreach($reserved_word_list as $word => $rep_string){
 
	if(strpos($some_text, $word)){
 
		// link the word
		$some_text = preg_replace('/(\s+)('.preg_quote($word).')/i','$1<a href="'.$rep_string.'">$2</a>',$some_text);
 
	}
}
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7