Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg problem 2

Status
Not open for further replies.

OsakaWebbie

Programmer
Feb 11, 2003
628
JP
As far as I can tell by searching documentation, a comma in regular expressions is not a special character, so if you want to find the first comma, you should be able to just say:
Code:
ereg(",",$myvariable)
But in my current code, for some reason, ereg is always returning 1, even though the comma is much later in the string (the data is just names: "Last, First"). Here is the whole line of code (including diagnostics in parentheses):
Code:
echo $title_class . "(".$main_mem->Furigana.":".ereg(",",$main_mem->Furigana).") ".substr($main_mem->Furigana, 0, ereg(",",$main_mem->Furigana)) . "</p>\n";
I get the following output (copied from source code view in browser):
Code:
<p class="household_title">(Bedow, Mark:1) B</p>
Any clues?
 
Please, is there anyone that has any thoughts on this? I'm stuck! I've never had ereg() misbehave like this before, although of course it's most likely to be the programmer's fault, not the language itself...[banghead] So can anyone see what this programmer is doing wrong?
 
If you just want to find the postion of the first comma in a string then I would use strpos(), it's faster than using a regular expression. Only use regular expressions when you are searching for a pattern, not a literal string.

Itshim
 
I'm not concerned about speed in this situation, but fortunately strpos does give me the right answer, so I'll use it. Perhaps we will never know why ereg didn't work - speed aside, it seems that it should have returned a 5, not a 1 - will mysteries never cease...
 
hmm, read this from manual: Returns the length of the matched string if a match for pattern was found in string

so in this case the matched string was "," -> length 1
so the substr returns only first char.
 
Oops, I didn't realize that! I was quite sure I had used it to find the position before, but maybe my memory was faulty. Thanks! (Hmm, I wonder if others didn't realize that either - it was a long time before anyone responded to my apparently silly question...) I'll give you a star, too - I like to reward people who are awake! :)
 
Thank you for the star!

This was a good lesson for me too, always learning something new. The ereg's and preg's are not so familiar for me, hated them in the beginning 'cause those patterns seemed so cryptic :) So I'm glad I was able to help.
 
Oh, those patterns may be cryptic, but they will do wonders when nothing else will do! Ever tried to, for example, check a string to see if it's at least got a fighting chance of being a valid email address? A string of at least one character that consists only of alphanumerics, periods, underscores, or hyphens, followed by a mandatory at-sign, followed by another string like the first, followed by a mandatory period, followed by a string of two, three, or four alphabetic-only characters... try that without regexp!
 

I tend to use the one below for matching email addresses, but then I dont need to be 100% accurate (although I'm not aware of it failing so far).

preg_match('/^[A-z0-9][\w.-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,6}$/',$email,$matches);
echo $matches[0];

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top