Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Smile strlen and preg_replace in same if wont work

    Hi
    I have made the following that works:

    PHP Code:
    $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:
    PHP Code:
    if (strlen(preg_replace("/[\D]/","",$phone) < 8)) {
        
    $errormassage .= "Must be 8 digits.";
        
    $error true; } 
    Anny body taht can explain why ??
    Last edited by Jesper Møller; 02-15-2010 at 06:16 AM.
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    That code worked fine when I tested it yesterday. What happens if you change this:

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

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

  • #3
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Stil the same

    first check always says its ok ??

    PHP Code:
    <?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 ' )
    Last edited by Jesper Møller; 02-15-2010 at 04:19 AM.
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #4
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    tryde

    PHP Code:
    $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
    PHP Code:
    $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
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Parantheses in the wrong place.

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

  • #6
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    135
    Thanks
    9
    Thanked 0 Times in 0 Posts
    THANK you MattF


    Mayby i shud take off my sunglasses
    (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 )
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •