...

View Full Version : Resolved strlen and preg_replace in same if wont work



Jesper Møller
02-15-2010, 04:35 AM
Hi
I have made the following that works:


$phonecheck2 = preg_replace("/[\D]/","",$phone);
if (strlen($phonecheck2) < 8) {
$errormassage .= "Must be 8 digits.";
$error = true; }

but when i try to combine the strlen and preg_replace it wont work:

if (strlen(preg_replace("/[\D]/","",$phone) < 8)) {
$errormassage .= "Must be 8 digits.";
$error = true; }

Anny body taht can explain why ??

MattF
02-15-2010, 05:08 AM
That code worked fine when I tested it yesterday. What happens if you change this:



if (strlen(preg_replace("/[\D]/","",$phone) < 8)) {
$errormassage .= "Must be 8 digits.";
$error = true; }


to this:



if (strlen(preg_replace('/[\D]/', '', $phone) < 8)) {
exit('Must be 8 digits.');
}

Jesper Møller
02-15-2010, 05:17 AM
Stil the same

first check always says its ok ??


<?php
$phone = '+1234 567';
echo 'checknumber: '.$phone.'<br>';

// Check 1
$phonecheck1 = preg_replace('/[\D]/','', $phone);
echo 'number: '.$phonecheck1.'<br>';
if (strlen(preg_replace('/[\D]/','',$phone) < 8)) {
echo 'Check1 = Is NOT 8 or more'; }
else{
echo 'Check1 = Is 8 or more'; }

echo '<br>';

// Check 2
$phonecheck2 = preg_replace('/[\D]/','',$phone);
echo 'number: '.$phonecheck2.'<br>';
if (strlen($phonecheck2) < 8) {
echo 'Check2 = Is NOT 8 or more'; }
else {
echo 'Check2 = Is 8 or more'; }
?>

(is ther any special rules for when to use " and ' )

Jesper Møller
02-15-2010, 06:59 AM
tryde


$phonecheck1 = preg_replace('/[\D]/','', $phone);
echo 'number: '.$phonecheck1.'<br>';
if (strlen(preg_replace('/[\D]/','',$phone) < 8)) {
exit('Must be 8 digits.');}
else{
echo 'Check1 = Is 8 or more'; }

and

$phonecheck1 = preg_replace('/[\D]/','', $phone);
echo 'number: '.$phonecheck1.'<br>';
if (strlen(preg_replace('/[\D]/','',$phone) < 8)) {
exit('Must be 8 digits.');}

non of them workd :(
get Check1 = Is 8 or more in number one nothing in number 2

MattF
02-15-2010, 07:05 AM
Parantheses in the wrong place.



if (strlen(preg_replace('#[\D]#', '', $phone)) < 8)

Jesper Møller
02-15-2010, 07:14 AM
THANK you MattF :thumbsup:


Mayby i shud take off my sunglasses :cool:
(Shud have been able to find that one myself)

Think it looks mutch better in one line than to
(And i have heard taht looks is important :p )



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum