help with preg_match please

Discussion in 'PHP' started by jacka, Oct 30, 2008.

  1. #1
    Hi there
    I want to separate the first part of the postcode (up to the space) from the rest.

    Please bear in mind that first part of a postcode can be 3 or 4 characters long.

    I have written this php code, which is wrong.
    
    $code=$_POST["pcode"];
    			
    			$data= ( preg_match('/^[a-zA-Z0-9]+[[$:space:]]/', $code) );
    			
    			
    			echo $data;
    
    Code (markup):

    can some one please help.
    thx in advance.
    :confused::confused:
     
    jacka, Oct 30, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    If there is definitely doing to be a space, why not just use explode?

    
    $code = $_POST["pcode"];
    $data = explode(' ', $code);
    print_r($data);
    
    PHP:
    Sorry if I've missed the point :rolleyes:

    Jay
     
    jayshah, Oct 30, 2008 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi
    No you haven't missed the point and in theory should have solved the problem but I get this message when I enter the postcode FD5 2DE:

    Array ( [0] => FG5 [1] => 2DE

    any ideas please?
    thx
     
    jacka, Oct 30, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    That's just outputting the array. This should guide you:

    
    echo "First part: ";
    echo $data[0];
    
    echo "First second: ";
    echo $data[1];
    
    PHP:
     
    jayshah, Oct 30, 2008 IP
  5. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi Got it.
    I used:
    
    echo $data[0];
    
    Code (markup):
    That gave me the first part of the code up to space.

    many thx
     
    jacka, Oct 30, 2008 IP