Welcome to WebmasterWorld Guest from 54.196.145.173

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regular expression help wanted

How to get at bit not matched

     
6:54 pm on Nov 20, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 14, 2003
posts:42
votes: 0


I am still quite new to PHP and I simply cannot find out how to do this.

I have a situation where I have the following file name to manipulate in code.

variablebitnnn.extension

where nnn could be a number from 1 into the thousands and where variablebit could be alphas and numerics even with a numeric on the end.

I need to get at the nnn.extension bit.

Now in perl this would be easy you would to a regexp to match the variablebit and the rest would be in $'

Now how do I achieve this in PHP?

7:32 pm on Nov 20, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


I'm assuming you are going to use a function like preg_match [php.net]? If you have a regex to match the variablebit, just add a second parenthesized subpattern to match the non-variablebit. Then use the
array matches
parameter to pull out the part you want. Make sense?
7:38 pm on Nov 20, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


$filename = "variablebitnnnn.ext";
$var = "variablebit";
$num_plus_ext = preg_replace("/^$var/","",$filename);
10:29 pm on Nov 20, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 14, 2003
posts:42
votes: 0


Thanks! That works perfectly.
10:39 pm on Nov 20, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


Neat way to do it DrDoc.