Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Storing part of a string in a new variable

9:34 am on Apr 24, 2007 (gmt 0)

5+ Year Member


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.


my strings are like this: "http://www.foo.com/directory/add/00102020/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.


11:18 am on Apr 24, 2007 (gmt 0)

5+ Year Member

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.

12:25 pm on Apr 24, 2007 (gmt 0)

5+ Year Member


I'll give it a try now.



Featured Threads

Hot Threads This Week

Hot Threads This Month