Regex to match any single alphabet in string php -
i hate code. take zipcode html pages verifies if zipcode usa or canada.
<?php $contents = file_get_contents('853755.html'); preg_match_all('/<td id="u_zipcode">(.*?)<\/td>/s', $contents, $matches); $data = implode("|",$matches[0]); $string = str_replace(' ', '', $data); if(preg_match('/^[1-9][0-9]*$/',$string)){ echo "canada"; } else { echo "usa"; } ?>
i tried had search solution it. can't find solution. tried regex should contain numbers not single alphabet. doesn't seem work in way. zipcode given in format,
usa: 97365 usa: 97365-97366 canada: j8n7s1 canada: n2l5y6
kindly me solution. thanks
since there can 1 element particular id, don't need use preg_match_all()
, can use preg_match()
.
preg_match('/<td id="u_zipcode">(.*?)<\/td>/s', $contents, $match);
then don't need use implode()
. part matches capture group in element 1 of match
array.
$string = str_replace(' ', '', $match[1]); if(preg_match('/^\d{5}(-\d{4})?$/',$string)) { echo "usa"; } else { echo "canada"; }
Comments
Post a Comment