Return to Snippet

Revision: 6793
at June 17, 2008 19:31 by pateketrueke


Updated Code
/**
 * (contexto[, busqueda[, reemplazo[, ordinal]]])
 *
 * Resalta palabras dentro de una cadena de texto.
 */
function search($text, $find, $repl = '<strong>\\1</strong>', $ord = 32)
{
	global $tags, $num;
	
	$num = 0; // contador
	$tags = array(); // pila
	
	$char = is_numeric($ord)?
		chr($ord): $ord[0]; // caracter?
	
	if (!function_exists('_search_backup_tags'))
	{ // guardamos el HTML
		function _search_backup_tags($test)
		{
			global $tags, $num;
			 // guardamos el tag completo
			$tags[$num] = $test[0];
			
			$tag = "<!$num>";
			$num++; // pisa y corre!
			return $tag;
		}
	}
	if (!function_exists('_search_restore_tags'))
	{ // recuperamos el HTML
		function _search_restore_tags($test)
		{
			global $tags; // xD
			return $tags[$test[1]];
		}
	}
	
	$text = // escapamos tags tipo HTML/BBCode
	preg_replace_callback('/([<\[][^>\]]+\/?[\]>])/', '_search_backup_tags', $text);
	
	// ----------------------------- Resaltamos!
	$found = array();
	$word = // separamos en palabras?
		explode($char, $find);
	
	foreach ($word as $test)
	{ // Escapamos las "wildcards"
		$found[] = preg_quote(strip_tags($test));
	}
	
	$expr = join('|', $found); // unimos
	$text = // Aqui ocurre toda la magia....
		preg_replace("/($expr)/is", $repl, $text);

	
	// recuperamos etiquetas HTML/BBCode
	$text = preg_replace_callback('/<!(\d+)>/', '_search_restore_tags', $text);
	
	return $text;
}

Revision: 6792
at June 17, 2008 19:15 by pateketrueke


Updated Code
/**
 * (contexto[, busqueda[, reemplazo[, ordinal]]])
 *
 * Resalta/Reemplaza palabras dentro de una cadena de texto.
 */
function search($text, $find, $repl = '<strong>\\1</strong>', $ord = 32)
{
	global $tags, $num;
	
	$num = 0; // contador
	$tags = array(); // pila
	
	$char = is_numeric($ord)?
		chr($ord): $ord[0]; // caracter?
	
	if (!function_exists('_search_backup_tags'))
	{ // guardamos el HTML
		function _search_backup_tags($test)
		{
			global $tags, $num;
			 // guardamos el tag completo
			$tags[$num] = $test[0];
			
			$tag = "<!$num>";
			$num++; // pisa y corre!
			return $tag;
		}
	}
	if (!function_exists('_search_restore_tags'))
	{ // recuperamos el HTML
		function _search_restore_tags($test)
		{
			global $tags; // xD
			return $tags[$test[1]];
		}
	}
	
	$text = // escapamos tags tipo HTML/BBCode
	preg_replace_callback('/([<\[][^>\]]+\/?[\]>])/', '_search_backup_tags', $text);
	
	if (is_array($find) || is_array($repl))
	{ // Reemplazo habitual...
		$text = preg_replace($find, $repl, $text);
	}
	else
	{ // ----------------------------- Resaltamos!
		$found = array();
		$word = // separamos en palabras?
			explode($char, $find);
		
		foreach ($word as $test)
		{ // Escapamos las "wildcards"
			$found[] = preg_quote(strip_tags($test));
		}
		
		$expr = join('|', $found); // unimos
		$text = // Aqui ocurre toda la magia....
			preg_replace("/($expr)/is", $repl, $text);
	}
	
	// recuperamos etiquetas HTML/BBCode
	$text = preg_replace_callback('/<!(\d+)>/', '_search_restore_tags', $text);
	
	return $text;
}

Revision: 6791
at June 17, 2008 19:13 by pateketrueke


Updated Code
/**
 * (contexto[, busqueda[, reemplazo[, ordinal]]])
 *
 * Resalta palabras dentro de una cadena de texto.
 */
function search($text, $find, $repl = '<strong>\\1</strong>', $ord = 32)
{
	global $tags, $num;
	
	$num = 0; // contador
	$tags = array(); // pila
	
	$char = is_numeric($ord)?
		chr($ord): $ord[0]; // caracter?
	
	if (!function_exists('_search_backup_tags'))
	{ // guardamos el HTML
		function _search_backup_tags($test)
		{
			global $tags, $num;
			 // guardamos el tag completo
			$tags[$num] = $test[0];
			
			$tag = "<!$num>";
			$num++; // pisa y corre!
			return $tag;
		}
	}
	if (!function_exists('_search_restore_tags'))
	{ // recuperamos el HTML
		function _search_restore_tags($test)
		{
			global $tags; // xD
			return $tags[$test[1]];
		}
	}
	
	$text = // escapamos tags tipo HTML/BBCode
	preg_replace_callback('/([<\[][^>\]]+\/?[\]>])/', '_search_backup_tags', $text);
	
	if (is_array($find) || is_array($repl))
	{ // Reemplazo habitual...
		$text = preg_replace($find, $repl, $text);
	}
	else
	{ // ----------------------------- Resaltamos!
		$found = array();
		$word = // separamos en palabras?
			explode($char, $find);
		
		foreach ($word as $test)
		{ // Escapamos las "wildcards"
			$found[] = preg_quote(strip_tags($test));
		}
		
		$expr = join('|', $found); // unimos
		$text = // Aqui ocurre toda la magia....
			preg_replace("/($expr)/is", $repl, $text);
	}
	
	// recuperamos etiquetas HTML/BBCode
	$text = preg_replace_callback('/<!(\d+)>/', '_search_restore_tags', $text);
	
	return $text;
}

Revision: 6790
at June 17, 2008 19:12 by pateketrueke


Updated Code
/**
 * (contexto[, busqueda[, reemplazo[, ordinal]]])
 *
 * Resalta palabras dentro de una cadena de texto.
 */
function search($text, $find, $repl = '<strong>\\1</strong>', $ord = 32)
{
	global $tags, $num;
	
	$num = 0; // contador
	$tags = array(); // pila
	
	$char = is_numeric($ord)?
		chr($ord): $ord[0]; // caracter?
	
	if (!function_exists('_search_backup_tags'))
	{ // guardamos el HTML
		function _search_backup_tags($test)
		{
			global $tags, $num;
			 // guardamos el tag completo
			$tags[$num] = $test[0];
			
			$tag = "<!$num>";
			$num++; // pisa y corre!
			return $tag;
		}
	}
	if (!function_exists('_search_restore_tags'))
	{ // recuperamos el HTML
		function _search_restore_tags($test)
		{
			global $tags; // xD
			return $tags[$test[1]];
		}
	}
	
	$text = // escapamos tags tipo HTML/BBCode
	preg_replace_callback('/([<\[][^>\]]+\/?[\]>])/', '_search_backup_tags', $text);
	
	if (is_array($find) || is_array($repl))
	{ // Reemplazo habitual...
		$text = preg_replace($find, $repl, $text);
	}
	else
	{ // ----------------------------- Resaltamos!
		$found = array();
		$word = // separamos en palabras?
			explode($char, $find);
		
		foreach ($word as $test)
		{ // Escapamos las "wildcards"
			$found[] = preq(strip_tags($test));
		}
		
		$expr = join('|', $found); // unimos
		$text = // Aqui ocurre toda la magia....
			preg_replace("/($expr)/is", $repl, $text);
	}
	
	// recuperamos etiquetas HTML/BBCode
	$text = preg_replace_callback('/<!(\d+)>/', '_search_restore_tags', $text);
	
	return $text;
}

Revision: 6789
at June 14, 2008 13:14 by pateketrueke


Initial Code
function search($text, $find, $ord = 32, $repl = '<strong>\\1</strong>')
{
	global $tags, $num;
	
	$num = 0; // counter!
	$tags = array(); // stack
	
	$char = is_numeric($ord)?
		chr($ord): $ord[0]; // char, separator
	
	if (!function_exists('_search_backup_tags'))
	{ // backup tags?
		function _search_backup_tags($test)
		{
			global $tags, $num;
			 // saved the full tag!
			$tags[$num] = $test[0];
			
			$tag = "<!$num>";
			$num++; // OK
			return $tag;
		}
	}
	if (!function_exists('_search_restore_tags'))
	{ // restore the backup
		function _search_restore_tags($test)
		{
			global $tags; // xD
			return $tags[$test[1]];
		}
	}
	
	$text = // escape tags "expression"
	preg_replace_callback('/([<\[][^>\]]+\/?[\]>])/', '_search_backup_tags', $text);
	
	// ----------------------------- Search highlight!
	$found = array();
	$word = // get the words
		explode($char, $find);
	
	foreach ($word as $test)
	{ // escape "wildcards"
		$found[] = preg_quote(strip_tags($test));
	}
	
	$expr = join('|', $found); // cool
	$text = // Magix happens there
		preg_replace("/($expr)/is", $repl, $text);
	
	// restore with the backup handler
	$text = preg_replace_callback('/<!(\d+)>/', '_search_restore_tags', $text);
	
	return $text;
}

Initial URL

                                

Initial Description

                                

Initial Title
Highlight within HTML/BBCode without source breaking

Initial Tags
php, html, replace

Initial Language
PHP