Re: Regex help

From: Date: Mon, 15 Mar 2004 18:05:08 +0000
Subject: Re: Regex help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-180484@lists.php.net to get a copy of this message
On 15 Mar 2004 Eric Gorr wrote: > >which will have a value like: 98797-234234--2c-something-2c > > > >How do I take out the part which will always start with "--2c" and will > >always end with "-2c" > > I'd be interested in the answer to this question as well. > Seems like it should be easy. It is easy. I agree with whoever said string functions were a better solution, regexp is overkill unless there is more variaiton in the data than what was presented. That said, here are some possibilities; all but the first assume that the "--" is always there: - If the lengths are constant just use substr to pull out the first 11 characters. - Find the "--" with strpos and then use that in a substr call to take just the part to the left. - Use explode to separate the string at the "--" or "--2c". - For two numeric strings separated by a dash use a regexp approach something like this: if (preg_match("/^(\d*-\d*)--2c/", $string, $matches)) $left_part = $matches[1]; -- Tom

« previous php.general (#180484) next »