Welcome to WebmasterWorld Guest from 54.221.87.97

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Storing part of a string in a new variable

     

breham

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

5+ Year Member



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

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.

breham

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

5+ Year Member



excellent,

I'll give it a try now.

Brett