homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Storing part of a string in a new variable
breham

5+ Year Member



 
Msg#: 3319762 posted 9:34 am on Apr 24, 2007 (gmt 0)

Hi,

I have a string (usually a url as it happens) which I need to get a certain part of and then store it in a variable.

i.e.

my strings are like this: "http://www.foo.com/directory/add/00102020/do.php?stuff"
"http://www.foo.com/directory/add/00301090/do.php?stuff"

So basically the URL or String are the same except for the numbers change. It is the numbers I want to store in a new variable.

I think I need to use regular expressions and preg_replace but just can't get my head around how!

Any help would be much appreciated.

Thanks
Brett

 

joelgreen

5+ Year Member



 
Msg#: 3319762 posted 11:18 am on Apr 24, 2007 (gmt 0)

Following will find number 4 to 10 characters long.
$url = 'http://www.example.com/123/directory/add/00102020/do.php?stuff';

$found = preg_match('/[0-9]{4,10}/',$url,$number);
if ($found) echo $number[0];

Will output 00102020 since 123 is only 3 chars long.

breham

5+ Year Member



 
Msg#: 3319762 posted 12:25 pm on Apr 24, 2007 (gmt 0)

excellent,

I'll give it a try now.

Brett

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