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 12 of 12
  1. #1
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Smile ereg_replace works preg_replace dont work?

    I have the following line taht works:
    PHP Code:
    $phonecheck ereg_replace("[ \+]"""$phone); 
    but if i try to use preg_replace insted it dont work.
    PHP Code:
    $phonecheck preg_replace("[ \+]"""$phone); 
    why ?? what do i do wrong ??
    Last edited by Jesper Møller; 02-15-2010 at 04:06 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 Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 342 Times in 338 Posts
    works for me, though.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    PHP Code:
    $phonecheck preg_replace('#[ \+]#'''$phone); 

  • #4
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks Matt
    thatt got me on track :-)

    looks like both #[ \+]#,/[\+\sa-z]/ and ([\+\s]) works :-)
    "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
    The simplest expression would be:

    Code:
    '#[^\d]#'
    Anything which isn't a number will be ditched.

  • #6
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Nice :-)

    Thought that ^ only was finding stuf the beginning of a string
    wow i got a lot to learn :-P

    Thanks a lot !

    Maby you can help me out with the next step ??

    I want to check if ther is minimum 8 charecters in the line

    This works:
    PHP Code:
    $phonecheck preg_replace("#[^\d]#","",$phone);
        if (
    strlen($phonecheck) < 8) {
            
    $error true; } 
    but this dont ?:
    PHP Code:
    if (strlen(preg_replace("#[^\d]#","",$phone) < 8)) {
        
    $error true; } 
    Last edited by Jesper Møller; 02-13-2010 at 08:13 PM.
    "True knowledge exists in knowing that you know nothing."

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

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 342 Times in 338 Posts
    how about \D instead of [^\d], pretty much the same.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    $phone isn't set.

    Thought that ^ only was finding stuf the beginning of a string
    When at the start of a character class, it negates the class. i.e: anything not in the class matches.
    Last edited by MattF; 02-13-2010 at 08:28 PM.

  • #9
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Dormilich View Post
    how about \D instead of [^\d], pretty much the same.
    Good point.

    Jesper, you may find this link useful for general reference:

    http://www.regular-expressions.info/

  • #10
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Funny ... i was actualy goin to ask abuth \d that :-)

    i have been using http://www.phpro.org/tutorials/Intro...P-Regex.html#6 to try to learn this and that said tha \d is Matches any numeric character and \D is Matches any non-numeric character
    but the ^ has been a litle kunfusing for me .. but as i see it if ^ is outside ^[] it meens "at the start" and if its inside [^] it meens "is not" ??

    Matt:
    Thanks for the link its now a bookmark , it looks a little more idept than the one i was looking at :-) (And a little more confusing) ..

    I can see i have a lot to learn
    "True knowledge exists in knowing that you know nothing."

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

  • #11
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    $phone isn't set.

    Yes it is (youst not shown in my first message)

    I have tryde both version in the same and no matter what i try check 1 comes out as if ther is 8 or more number

    PHP Code:
    <?php
    $phone 
    '+1234 567';

    // 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 1
    $phonecheck2 preg_replace("#[\D]#","",$phone);
    if (
    strlen($phonecheck2) < 8) {
        echo 
    'Check2 = Is NOT 8 or more'; }
    else {
        echo 
    'Check2 = Is 8 or more'; }
    ?>
    Last edited by Jesper Møller; 02-13-2010 at 11:04 PM.
    "True knowledge exists in knowing that you know nothing."

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

  • #12
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Well my original question was answered, So i will mark it as resolved



    The part of the working and not working code must be a new questio ? so i have made a new tread for it strlen and preg_replace in same if wont work

    Thanks for the help, you guys rock
    "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
    •