Connecting Tech Pros Worldwide Forums | Help | Site Map

preg_match issue

Henri
Guest
 
Posts: n/a
#1: Sep 24 '07
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?

Captain Paralytic
Guest
 
Posts: n/a
#2: Sep 24 '07

re: preg_match issue


On 24 Sep, 13:52, Henri <h...@dontbother.fuwrote:
Quote:
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?

gosha bine
Guest
 
Posts: n/a
#3: Sep 24 '07

re: preg_match issue


Henri wrote:
Quote:
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
Henri
Guest
 
Posts: n/a
#4: Sep 24 '07

re: preg_match issue


On Mon, 24 Sep 2007 06:31:28 -0700, Captain Paralytic wrote:
Quote:
On 24 Sep, 13:52, Henri <h...@dontbother.fuwrote:
Quote:
>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.
Quote:
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.


Closed Thread