Re: regular expressions gods! (fwd)
| From: | jeremy brand | Date: | Wed, 23 Aug 2000 04:56:15 +0000 |
| Subject: | Re: regular expressions gods! (fwd) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-13080@lists.php.net to get a copy of this message | ||
> Jeremy,
>
> Thanks heaps, i'll give it a try this afternoon :)
>
> It's sort of what I thought it's look like, but i had hoped that it
> would NOT put the prefix onto pages other than .php3, and that it would
> not affect pages not in the current directory.
That is correct, what you say will happen. I'm not the reg-ex god
either -- but since nobody else answered, I figured the first thing
off the top of my head would be better then nothing.
I would say, sed and awk would be a better set of tools for doing this
than PHP.
Here, as far as I can tell, you might need more than one regex (plus
use matches from one rexex in another regex for the replacement).
So, here is something that I think does what you want:
-- snip --
$array[] = './this/is/1/test.php3';
$array[] = './this/is/2/test.php3';
$array[] = './this/is/3/test.html';
$array[] = './this/is/4/test.php3';
for ($i=0; $i<count($array); $i++)
{
ereg('([A-Za-z0-9]*).php3', $array[$i], $hits);
$array[$i] = eregi_replace($hits[0], 'link_'.$hits[0], $array[$i]);
print "\$array[$i] = " . $array[$i] . "\n";
}
-- snip --
The output of this is:
$array[0] = ./this/is/1/link_test.php3
$array[1] = ./this/is/2/link_test.php3
$array[2] = ./this/is/3/test.html
$array[3] = ./this/is/4/link_test.php3
Note: the match ([A-Za-z0-9]) you will also want to fill with
possible characters that your files may have.
Cheers,
-jeremy brand
_______________________________________________________________________
GnuPG Fingerprint: 9F5E 95E0 1CC2 E054 84EA 6099 8ACD 9DBA D2C0 5EA9
_ http://www.JeremyBrand.com/Jeremy/Brand/Jeremy_Brand.html
for more __