php regular expressions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simbarashe
    New Member
    • Nov 2006
    • 7

    php regular expressions

    Hie

    could someone please help me> I'm trying to use a regular expression to validate and address and it is as follows.

    eregi('(^[a-zA-Z0-9 \\\.,-]+)$',$value4)

    $value4 = 10/54 kent street;

    I want to allow the users to put in slashes as part of their addresses so I'm looking 4 a simple regular expression to do that.

    Thank you
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Originally posted by simbarashe
    Hie

    could someone please help me> I'm trying to use a regular expression to validate and address and it is as follows.

    eregi('(^[a-zA-Z0-9 \\\.,-]+)$',$value4)

    $value4 = 10/54 kent street;

    I want to allow the users to put in slashes as part of their addresses so I'm looking 4 a simple regular expression to do that.

    Thank you
    If I'm not very much mistaken, putting a '/' in front of your '\\\' would do the trick.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      You'll probably want something like this:

      [code=php]
      preg_match('/^[\d]+(\/[\d]+)?(\b\w)+$/', $value4);
      [/code]

      That would most closely match '10/54 kent street'.

      The regular expression there matches at least one number, then optionally (a slash followed by at least one number), then at least one word.

      If you want to be able to do something like '10/54 west 3rd street', you'll have to modify it slightly (assumes that street names only start with numbers and never have numbers in the middle or end):

      [code=php]
      preg_match('/^[\d]+(\/[\d]+)?(\b\d*\w)+$/', $value4);
      [/code]

      For more info:
      Reference of the various syntactic elements that can appear in regular expressions

      Comment

      • simbarashe
        New Member
        • Nov 2006
        • 7

        #4
        Originally posted by pbmods
        You'll probably want something like this:

        [code=php]
        preg_match('/^[\d]+(\/[\d]+)?(\b\w)+$/', $value4);
        [/code]

        That would most closely match '10/54 kent street'.

        The regular expression there matches at least one number, then optionally (a slash followed by at least one number), then at least one word.

        If you want to be able to do something like '10/54 west 3rd street', you'll have to modify it slightly (assumes that street names only start with numbers and never have numbers in the middle or end):

        [code=php]
        preg_match('/^[\d]+(\/[\d]+)?(\b\d*\w)+$/', $value4);
        [/code]

        For more info:
        http://www.regular-expressions.info/reference.html
        I tried that script but it didnt do the trick, thanks for it though maybe I needed to twist and turn a little bit to make it suitable for my use. I managed to use the script that i had made before but however I have encountered and even bigger problem and it is as follows:

        Could someone please help me with my regular expresssions because my script is behaving in a rather wierd way when I implement the regular expressions. I am checking names and addresses before I insert them into my database but the regular expression function is not working when the MYSQL INSERT QUERIES are running. Below is my script and I'm just going to briefly explain it. The isWord($value,$ pageURL) funtion is in a file called dbFunction.php and its as follows:

        [code=php]
        function isWord($value,$ pageURL){
        $value2 = trim(stripslash es($value));
        f((strlen($valu e2) == 0) or (!ereg("^[[:alpha:]'-]{1,100}$",$valu e2))){
        $uri = "//" . $_SERVER["SERVER_NAM E"]. rtrim(dirname($ _SERVER['PHP_SELF']), '/\\');
        header("Locatio n: http:".$uri.$pa geURL."&err=1") ;
        }else{
        return $value2;
        }
        }[/code]

        [Please use CODE tags when posting source code. Thanks! --pbmods]

        I then called it in dbJobsheet as follows:

        [code=php]
        $firstName = isWord($_POST['firstName'],$errorPageURL) ;[/code]

        this works perfectly because if I put in invalid characters I get redirected to the error page. However when I put in the script to insert the data into the database the above isWord($value,$ pageURL) doesnt work.

        the script that stops all this from working is as follows:

        [code=php]<?
        $timeQuery = mysql_query("SE LECT date_assinged,t ime_assigned,as sStaffID

        FROM FOLLOW_UP WHERE
        time_assigned ='$newTime' AND
        date_assinged = '$newAssignedDa te'
        AND assStaffID = '$assStaffID' ")
        or die(mysql_error ());

        $results = mysql_fetch_arr ay($timeQuery);
        //checks if there is a time clash and redirects user to the page to re-enter the information.
        if ((count($result s)-1)>0){
        header('Locatio n: index.php?
        page=redirectOl dClientsJobShee t.php&err=6'); }

        //checks is the contact exists
        elseif(mysql_nu m_rows(mysql_qu ery("SELECT contactID FROM
        CONTACTS WHERE first_name = '$firstName'
        AND last_name = '$lastName' AND
        email_address = '$email_address ' "))){

        mysql_query("IN SERT INTO STATUS VALUES(
        NULL,'$status', '$description') ")
        or die (mysql_error()) ;

        $statusID = mysql_query("SE LECT statusID FROM STATUS WHERE
        statusID = 'LAST_INSERT_ID ()' ") or die (mysql_error()) ;

        mysql_query("IN SERT INTO JOBSHEET VALUES(
        NULL,'$clientID ',LAST_INSERT_I D(),'$staffID', '$COD','$billed ',
        CURRENT_DATE(), '$warranty','$j obID','$contact ID','$title',
        '$complete','$l og')") or die (mysql_error()) ;

        header('Locatio n: index.php?page= oldClientsEnter edInfoDisplay.p hp');
        }
        else{
        header('Locatio n: index.php?page= redirectOldClie ntsJobSheet.php &err=7');
        }
        }
        ?>[/code]
        Last edited by pbmods; May 21 '07, 07:38 PM. Reason: Added code tags.

        Comment

        Working...