![]() Topic awaiting preservation: Regex (again) (Page 1 of 1) |
|
|---|---|
|
Paranoid (IV) Inmate From: 1393 |
posted 02-20-2009 17:26
Hello everyone... I know I only show up when I need help but I figure I'll just keep tapping your knowledge until you say when. code: <p><a href="test.html" title="Test titles">Test your skills</a> and test some more</p> Doing a desired preg_replace on the above code snippet for "test" I would want to only replace the 3rd and 4th occurrence (nothing within an HTML attribute). code: (?<=^|>)[^><]+?(?=<|$) Any help/pointers would be much appreciated... peace. |
|
Paranoid (IV) Inmate From: cell 3736 |
posted 02-21-2009 03:28
This is really a very simple thing conceptually and IMO not suitable for regex. |
|
Paranoid (IV) Inmate From: Florida |
posted 02-21-2009 04:07
|
|
Paranoid (IV) Inmate From: 1393 |
posted 02-21-2009 19:04
Thanks for the pointers... those links are just what I needed. Peace my brothers. |
|
Paranoid (IV) Inmate From: 1393 |
posted 02-24-2009 00:07
For those who may want to know or critique here's what I came up with (seems to be working properly): code: // Find match
preg_match("/" . preg_quote($keyword) . "/Uis", strip_tags($string), $match);
if(!empty($match[0]))
{
// Highlight entries
$length = strlen($string);
$new_string = "";
$cur_start = 0;
while($cur_start < $length)
{
$html_start = strpos($string, "<", $cur_start);
$html_end = ($html_start !== false) ? strpos($string, ">", $html_start) : NULL;
$inside_html = ($html_start !== false) ? (($cur_start >= $html_start && $cur_start <= $html_end) ? true : false) : false;
$cur_end = ($html_start !== false) ? (($inside_html) ? $html_end : $html_start) : $length;
$cur_string = substr($string, $cur_start, ($cur_end - $cur_start));
$new_string .= ($inside_html) ? $cur_string : preg_replace("/" . preg_quote($keyword) . "/Uis", "<span style=\"background-color: #ffff66;\">$0</span>", $cur_string);
$cur_start = $cur_end;
}
$new_data = $new_string;
}
|