w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Regex to match any single alphabet in string php

Since there can only be one element with a particular ID, you don't need to use preg_match_all(), you can use preg_match().

preg_match('/<td id="u_zipCode">(.*?)</td>/s',
$contents, $match);

Then you don't need to use implode(). The part that matches the capture group will be in element 1 of the match array.

$string = str_replace(' ', '', $match[1]);

if(preg_match('/^d{5}(-d{4})?$/',$string)) {
    echo "USA";
} else {
    echo "Canada";

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.