[thelist] regex help needed

Jay Blanchard jay.blanchard at niicommunications.com
Tue May 21 08:39:00 CDT 2002


[snip]
$match = array();
$string = "one two three UR12 four five six";
if (ereg ("[A-Z]{1,2}[0-9]{2,3}", $string, $match)) {
	for ($i=0; $i < count($match); $i++) {
		$new_match = '<a href="page.html?ref=' . $match[$i] . '">' . $match[$i] .
'</a>';
		$string = ereg_replace($match[$i], $new_match, $string);
	}
}
echo $string;

but i'm getting a count($match) of 10 and no joy on the ereg_replace.
[/snip]

I'd go through the process of adding print/echo statements to see the
problems....
<?php
$string = "one two three UR12 four five six EZ12";
$i = 0;
if (ereg ("[A-Z]{1,2}[0-9]{2,3}", $string, $match)) {
	print(count($match) . "<br>");
	print("This is the matched item:" . $match[$i] . "<br>");
	$new_match = ("<a href=\"page.html?ref=" . $match[$i] . "\">" . $match[$i]
. "</a>");
	print($new_match . "<br>");
	$string = ereg_replace($match[$i], $new_match, $string);
	echo $string;

}
?>
returns
10 (count($match))...only had one match, why return 10?
This is the matched item:UR12
UR12
one two three UR12 four five six EZ12

HTH!

Jay





More information about the thelist mailing list