View Full Version : Resolved combine strlen with preg, help please

02-18-2010, 04:05 AM
Hi... i would be like to say thank you and grateful for someone who consider to help me...

i want to change this code:

function isPhoneValid() {
// 5 digits number
return ereg('^[0-9]${5}', $this->phone);
with adding strlen.. so become >5 digit number...


02-18-2010, 04:14 AM
return ctype_digit( $this->phone ) && strlen( $this->phone ) >= 5;

02-18-2010, 06:55 AM
Yes, this work fine... thank you n GBU bro, but how to mark this thread solved?

02-18-2010, 02:24 PM
or just

return ereg('^[0-9]${5,}', $this->phone);

{5} means exactly five digits
{5,10} means between 5 and 10 characters
{5,} means 5 or more characters

Incidentally I would think it should be '^[0-9]{5,}$' as $ indicates the end of the data and so should be at the end of the pattern, may work the other way but seems less correct to me.

Caveat: I always use PCRE regex functions preg_ rather than ereg_ but assuming ereg_ supports {5} it should support {5,} as well.



02-18-2010, 02:31 PM
If you want to use preg_match(), it would be:

return ( bool ) preg_match( '/^\d{5,}$/', $this->phone );

And to mark the topic as resolved, I believe you'd edit it, and next to the subject line there should be a select-menu marked `Prefix`.

02-19-2010, 05:12 AM
oh... that's several ways to solved this..