homepage Welcome to WebmasterWorld Guest from 54.197.147.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Regular expression help wanted
How to get at bit not matched
neiljones




msg:1276493
 6:54 pm on Nov 20, 2003 (gmt 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?

 

coopster




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

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?
DrDoc




msg:1276495
 7:38 pm on Nov 20, 2003 (gmt 0)

$filename = "variablebitnnnn.ext";
$var = "variablebit";
$num_plus_ext = preg_replace("/^$var/","",$filename);

neiljones




msg:1276496
 10:29 pm on Nov 20, 2003 (gmt 0)

Thanks! That works perfectly.

coopster




msg:1276497
 10:39 pm on Nov 20, 2003 (gmt 0)

Neat way to do it DrDoc.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved