php array regular expressions

Posted by bell on Stack Overflow See other posts from Stack Overflow or by bell
Published on 2010-04-26T17:00:06Z Indexed on 2010/04/26 17:03 UTC
Read the original article Hit count: 217

Filed under:
|
|

I am using regular expressions in php to match postcodes found in a string.

The results are being returned as an array, I was wondering if there is any way to assign variables to each of the results, something like

$postcode1 = first match found
$postcode2 = second match found

here is my code

$html = "some text here bt123ab and another postcode bt112cd";
preg_match_all("/([a-zA-Z]{2})([0-9]{2,3})([a-zA-Z]{2})/", $html, $matches, PREG_SET_ORDER);

foreach ($matches as $val) {
    echo $val[0]; }

I am very new to regular expressions and php, forgive me if this is a stupid question.

Thanks in advance

© Stack Overflow or respective owner

Related posts about regex

Related posts about php