Logré encontrar el siguiente código, denominado linktext.php, que funciona a la perfección para lo que necesito yo:
Código :
<?
// ----------------------------------------------------------------------
// Linktext
// Copyright (C) 2006 by Ian Egerton
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Ian Egerton
// Purpose of file:
// ----------------------------------------------------------------------
// Function to format and return the main body text of each page
function body_text($body, $exclude)
{
// Declare some key words to be replaced with links. Replace these with keywords that you would like to use and add links in $param_links in a matching array space
$param_names = array("google", "working example", "www.irishchinacycle.com");
// Declare matching destination links for the key words.
$param_links = array("http://www.google.com", "http://www.irishchinacycle.com", "http://www.irishchinacycle.com");
// Array to hold stripped HTML tags
$html_text = array("");
// Array to hold matching special chars
$tag_text = array("");
// String to hold all bosy text
$t_text = "";
// Variable used to check status of body text
$in_tag = 0;
// Utility variables
$j = 0;
$end = 0;
$k = 0;
// Strip all HTML tags from source, store each HTML tag in an array and store corressponding special chars in another array for restoring the tags.
for ($start = 0; $start < strlen($body); $start++)
{
// Check for the start of a HTML tag
if (strcmp(substr($body, $start, 1), "<") == 0)
{
$in_tag=1;
$end = $start;
$tag_text[$k] = '[*?|-#00' . $k . '00#-|?*]';
$t_text .= $tag_text[$k];
$k++;
}
// Check for the end of a HTML tag
elseif (strcmp(substr($body, $start, 1), ">") == 0)
{
$num = $start-$end;
$in_tag=0;
$html_text[$j] = (substr($body, $end, ($num+1)));
$j++;
}
// Store reaming "real" text in a string with special chars replacing HTML tags
elseif ($in_tag == 0)
{
$t_text .= substr($body, $start, 1);
}
}
$flag = 0;
// Use regular expressions to find and replace key words with links
for ($i = 0; $i < sizeof($param_names); $i++)
{
// If you do not want certain words linked, you can declare exclusions within individual pages, see where body_text() is called within these files.
if($exclude[0])
{
for($z = 0; $z < sizeof($exclude); $z++)
{
if($exclude[$z] == $param_names[$i])
{
$flag = 1;
}
}
}
// If the replace words are not found in the exclusions list replace special chars on each end of the word/phrase and add relevant linkage data to the array.
if($flag != 1)
{
$tag_text[$k] = '{|-#00' . $k . '00#-|}';
$t_text = preg_replace('/\b(' . $param_names[$i] . ')\b/i', '' . $tag_text[$k] . '$1', $t_text);
$k++;
$tag_text[$k] = '{|-#00' . $k . '00#-|}';
$t_text = preg_replace('/\b(' . $param_names[$i] . ')\b/i', '$1' . $tag_text[$k], $t_text);
$k++;
$html_text[$j] = '<a href="' . $param_links[$i] . '">';
$j++;
$html_text[$j] = '</a>';
$j++;
}
$flag = 0;
}
// Replace the special chars with the proper html tags when finished.
for ($i = 0; $i < sizeof($html_text); $i++)
{
$t_text = str_replace($tag_text[$i], $html_text[$i], $t_text);
}
// Return the text to the function call
return $t_text;
}
// Simple implemented example of how the script works
$html_body = 'This is an example of the function being implemented.<br>A good search engine is google.<br>Another large working example of this code can be seen at www.irishchinacycle.com.';
// Pass all body content to body_text function in include file for dynamic link creation and keyword highlighting after search results
// Include words or phrases that you do not want included as links on a particular page. NOTE: These will only be words or phrases included in the "$param_names" array in the body_text() function.
$exclude = array("");
$html_body = body_text($html_body, $exclude);
// Display all main body including formatting
echo '' . $html_body . '';
?>
Ahora, el problema que tengo es que funciona perfecto, pero solo si el html está dentro de ese script:
Código :
// Simple implemented example of how the script works
$html_body = 'This is an example of the function being implemented.<br>A good search engine is google.<br>Another large working example of this code can be seen at www.irishchinacycle.com.';
Yo lo que quiero hacer, es incluirlo de alguna manera en mis páginas html, para que surta efecto sobre las palabras que haya en ellas.
Gracias!
PD: El script viene sin ningún tipo de readme, ni nada por el estilo.