Connecting Tech Pros Worldwide Forums | Help | Site Map

ereg question

Rafal Zak
Guest
 
Posts: n/a
#1: Jul 17 '05
I have a little problem with ereg (eregi) in PHP - in some cases it behaves
differently than I expect and I don`t know if my expectations are strange or
there is any "syntax subtlety" I have missed.

I want to find a value for key1 in string:

key1=>value_of key1[EOT]key2=>other_value[EOT]key3=>value[3][EOT]

and I use an ereg pattern

ereg("key1=>([[:print:][:space:]]*)\[EOT\]",$line,$regs);

unfortunately ereg match differs from "value_of key1", which I wanted to
receive. Ereg finds the last apperance of [EOT] in string $line and the
value of $regs[1] is

"key1=>value_of key1[EOT]key2=>other_value[EOT]key3=>value[3]"

Can I achieve an effect, that ereg matches the first occurence of [EOT] and
as a result I`'ll get "value_of key1" as a result?

Thanks for help
Rafal





Justin Koivisto
Guest
 
Posts: n/a
#2: Jul 17 '05

re: ereg question


Rafal Zak wrote:
[color=blue]
> I have a little problem with ereg (eregi) in PHP - in some cases it behaves
> differently than I expect and I don`t know if my expectations are strange or
> there is any "syntax subtlety" I have missed.
>
> I want to find a value for key1 in string:
>
> key1=>value_of key1[EOT]key2=>other_value[EOT]key3=>value[3][EOT]
>
> and I use an ereg pattern
>
> ereg("key1=>([[:print:][:space:]]*)\[EOT\]",$line,$regs);
>
> unfortunately ereg match differs from "value_of key1", which I wanted to
> receive. Ereg finds the last apperance of [EOT] in string $line and the
> value of $regs[1] is
>
> "key1=>value_of key1[EOT]key2=>other_value[EOT]key3=>value[3]"
>
> Can I achieve an effect, that ereg matches the first occurence of [EOT] and
> as a result I`'ll get "value_of key1" as a result?[/color]

The * in "([[:print:][:space:]]*)" will also match the first "[EOT]"
because they are all print characters. You'd need to use a negative
look-ahead or something along those lines, but I do not know if that is
supported in ereg patterns....

--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Matthew Crouch
Guest
 
Posts: n/a
#3: Jul 17 '05

re: ereg question


[color=blue]
>
> unfortunately ereg match differs from "value_of key1", which I wanted to
> receive. Ereg finds the last apperance of [EOT] in string $line and the
> value of $regs[1] is
>
> "key1=>value_of key1[EOT]key2=>other_value[EOT]key3=>value[3]"
>
> Can I achieve an effect, that ereg matches the first occurence of [EOT] and
> as a result I`'ll get "value_of key1" as a result?
>
> Thanks for help
> Rafal[/color]

The match is called "greedy" because it grabs the biggest chunk of
characters that meet your condition (print or space).

add to that condition a [^\[EOT\]] and you should get what you want
Michael Fesser
Guest
 
Posts: n/a
#4: Jul 17 '05

re: ereg question


.oO(Matthew Crouch)
[color=blue]
>The match is called "greedy" because it grabs the biggest chunk of
>characters that meet your condition (print or space).
>
>add to that condition a [^\[EOT\]] and you should get what you want[/color]

Why not simply use preg_match with the modifier U?

Micha
Closed Thread


Similar PHP bytes