PHP exact match between input and regex pattern -
i'm trying build check reliably evaluates whether input ($f_username) mac address via regex 'cause there different syntax take. upon finding match. should transferred lowercase without deliminators.
the function works fine in matching , transforming input, wrongly match longer input... e.g. 11-22-33-44-55-66-77-88 transferred 11-22-33-44-55-66 , $match set true...
this should cause function go "else branch" is not exact match of pattern... contains match... have idea how match ?
thanks taking time read , in advance answers :)
function username_check($f_username) { global $match; if (preg_match_all("/([0-9a-fa-f]{2})[^0-9a-fa-f]?([0-9a-fa-f]{2})[^0-9a-fa-f]?([0-9a-fa-f]{2})[^0-9a-fa-f]?([0-9a-fa-f]{2})[^0-9a-fa-f]?([0-9a-fa-f]{2})[^0-9a-fa-f]?([0-9a-fa-f]{2})/", $f_username, $output, preg_pattern_order)) { ($i = 1; $i <= 6; $i++) { $new_username .= strtolower($output[$i][0]); } $match = true; $new_username = "'" . $new_username . "'"; //for later use in sql-query } else { $match = false; } return $new_username; }
i recommend using the regex answer, ensures well-formed mac-address. if want add spaces list of delimiters, replace [-:]
[: -]
.
you experiencing problem described because haven't bound regex start, or end of string. means long there's match somewhere inside string it's valid match.
bind start of string, use ^
after opening delimiter. bind @ end of string, recommend* using \z
before closing delimiter.
* reason recommend \z
on $
, in php, because latter allow newline after match. means string "testing\n"
match pattern bound $
, not 1 bound \z
. of times not want newline.
Comments
Post a Comment