Forum Moderators: coopster

Message Too Old, No Replies

How to I filter out a value from a long string?

         

irock

8:09 pm on Oct 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wanted to retrieve a value that follows "PD-" in a string that is similar to this, "mystring/PD-12345" (w/o double quotes).

How do I get "12345" from this string? I know there's a code that allows me to do so, but it's been a long time and I forgot.

Could anyone help?

coopster

8:37 pm on Oct 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'd use preg_match [us4.php.net].

$string = 'mystring/PD-12345';
preg_match("/PD-(\d+)/", $string, $matches);
print $matches[1];

irock

9:41 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... doesn't seem to work.

My string value is something like this one.
[mydomain.com...]

I would like to retreive the value '21277586'

Your script works if it's "/PD-". Now that the string is "&PD=" When I replace it with...

preg_match("&PD=(\d+)/", $string, $matches);

I got this error.
Warning: Delimiter must not be alphanumeric or backslash

[edited by: irock at 9:46 pm (utc) on Oct. 20, 2003]

vincevincevince

9:45 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$text="http://www.mydomain.com/index.php?blah=123&PD=21277586";

preg_match("\PD=([0-9]*)\",$text,$matches);

echo $matches[1];

irock

9:48 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, vincevincevince!

Hmm I got parse error.

Something wrong with your preg_match line

irock

9:50 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



never mind... I fixed it by changing the slash from backward slash to forward slash.

Thanks!

Really appreciate your help. :)

MonkeeSage

9:53 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



preg_match('/PD.?(\d+)/', $string, $matches);

Jordan