By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,641 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,641 IT Pros & Developers. It's quick & easy.

ereg question

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rafal Zak wrote:
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?


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 - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #2

P: n/a

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


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
Jul 17 '05 #3

P: n/a
.oO(Matthew Crouch)
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


Why not simply use preg_match with the modifier U?

Micha
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.