preg_match issue
am 24.09.2007 14:52:52 von henri
Hello,
I am trying to split a string that contains parentheses; the aim here is
to keep the part that's before the opening parenthese:
the string (quoted here) is "36 (72 cm)"
First I want to know if the string contains at least an openning
parenthese; if it does, I split it and extract the first token (ie "36 ").
My code follows
if (preg_match("/\(/", $sPermutationName)) {
$tokens = explode("\(", $sPermutationName);
$sPermutationName = $tokens[0];
}
but that does not work. Would anyone care to enlighten me on this one?
Re: preg_match issue
am 24.09.2007 15:31:28 von Captain Paralytic
On 24 Sep, 13:52, Henri wrote:
> Hello,
>
> I am trying to split a string that contains parentheses; the aim here is
> to keep the part that's before the opening parenthese:
>
> the string (quoted here) is "36 (72 cm)"
> First I want to know if the string contains at least an openning
> parenthese; if it does, I split it and extract the first token (ie "36 ").
> My code follows
>
> if (preg_match("/\(/", $sPermutationName)) {
> $tokens = explode("\(", $sPermutationName);
> $sPermutationName = $tokens[0];
>
> }
>
> but that does not work. Would anyone care to enlighten me on this one?
"does not work", ahh how helpful! That tells us exactly what you are
actually seeing - NOT!!!
As a matter of interest, why are you exploding on "\(", when \ doesn't
appear in the target string?
Re: preg_match issue
am 24.09.2007 15:46:16 von gosha bine
Henri wrote:
> Hello,
>
> I am trying to split a string that contains parentheses; the aim here is
> to keep the part that's before the opening parenthese:
>
> the string (quoted here) is "36 (72 cm)"
> First I want to know if the string contains at least an openning
> parenthese; if it does, I split it and extract the first token (ie "36 ").
> My code follows
>
> if (preg_match("/\(/", $sPermutationName)) {
> $tokens = explode("\(", $sPermutationName);
> $sPermutationName = $tokens[0];
> }
>
> but that does not work. Would anyone care to enlighten me on this one?
Hi Henri
regular expressions would be on overkill here.
strtok($a, "(")
does exactly what you want - returns a portion of the string before "(".
If there's no "(" , the whole string will be returned.
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Re: preg_match issue
am 24.09.2007 16:42:19 von henri
On Mon, 24 Sep 2007 06:31:28 -0700, Captain Paralytic wrote:
> On 24 Sep, 13:52, Henri wrote:
>> Hello,
>>
>> I am trying to split a string that contains parentheses; the aim here
>> is to keep the part that's before the opening parenthese:
>>
>> the string (quoted here) is "36 (72 cm)" First I want to know if the
>> string contains at least an openning parenthese; if it does, I split it
>> and extract the first token (ie "36 "). My code follows
>>
>> if (preg_match("/\(/", $sPermutationName)) {
>> $tokens = explode("\(", $sPermutationName); $sPermutationName =
>> $tokens[0];
>>
>> }
>>
>> but that does not work. Would anyone care to enlighten me on this one?
>
> "does not work", ahh how helpful! That tells us exactly what you are
> actually seeing - NOT!!!
>
mmh... forgive me. What I clumsily tried to mean is that the preg_match
did not seem to see any parenthese in the string and therefore did not
execute the two following statements.
> As a matter of interest, why are you exploding on "\(", when \ doesn't
> appear in the target string?
because I thought that escaping the parenthese would avoid a compilation
error due to the code's being misinterpreted (ie: reporting a parenthese
mismatch).
Anyway, I think I have a solution now.
Re: preg_match issue
am 24.09.2007 17:21:33 von henri
On Mon, 24 Sep 2007 15:46:16 +0200, gosha bine wrote:
> Henri wrote:
>> Hello,
>>
>> I am trying to split a string that contains parentheses; the aim here
>> is to keep the part that's before the opening parenthese:
>>
>> the string (quoted here) is "36 (72 cm)" First I want to know if the
>> string contains at least an openning parenthese; if it does, I split it
>> and extract the first token (ie "36 "). My code follows
>>
>> if (preg_match("/\(/", $sPermutationName)) {
>> $tokens = explode("\(", $sPermutationName); $sPermutationName =
>> $tokens[0];
>> }
>>
>> but that does not work. Would anyone care to enlighten me on this one?
>
> Hi Henri
>
> regular expressions would be on overkill here.
>
> strtok($a, "(")
>
> does exactly what you want - returns a portion of the string before "(".
> If there's no "(" , the whole string will be returned.
how stupid of me! the parenthese was converted to its html entity! Doh