[thelist] regex help needed

Schoolcraft, Jeffrey SchoolcraftJ at SytexInc.com
Tue May 21 08:14:06 CDT 2002


Kris,

I went about it a little differently than you did, mine looks like this:

<?php

$string = "one two three UR12 four five six";
$string = ereg_replace("([A-Z]{1,2}[0-9]{2,3})","<a
href=\"page.html?ref=\\1\">\\1</a>", $string);
echo $string;

?>

Basically I'm doing the replace in 1 step, the \\1 is a back reference
to the first matching (group), delimited by parens.

This should do what you need though.

Jeff

> -----Original Message-----
> From: kris burford [midtempo] [mailto:kris at midtempo.net]
> Sent: Tuesday, May 21, 2002 6:59 AM
> To: thelist at lists.evolt.org
> Subject: [thelist] regex help needed
>
>
> hi all,
>
> delving into regex for the first time and i'm having issues (surprise,
> surprise!)
>
> i've got a cms system where the client can type in their
> product codes into the body text. i'm attempting to identify
> these codes and create urls to the respective products
> passing the code in the url. the codes are identified by
> either one or two capital letters followed by 2-3 digits.
>
> so far i've got
>
> $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. can anyone point me in the right direction? (if
> there's a better way to do this, i'd also be most grateful
> for any advice...)
>
> tia
>
> kris
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
http://lists.evolt.org Workers of the Web, evolt !



More information about the thelist mailing list