php - <br> tag inside reqular expression -
i have regular expression
$rexp = '/^[0-9a-za-z\s"]{1,22}$/';
and check
if(preg_match($rexp, $userstring)) {echo 'ok';}
my problem $userstring can this:
$userstring = 'hello<br>hello';
i modify regular look so:
$rexp = '/^[0-9a-za-z\s<>"]{1,22}$/';
but dont because accepts script , other tags
how can alter regular look accepts br tag?
thank you
im assuming sort of input validation.
the proposed reply @onlinecop work think messing "length" of string, since <br>
tag count 4 chars.
will work? yes, sure. it's expected outcome? im not sure.
if thats works you, excellent! think fastest method, very little overhead can this:
$maxlength = 22; // proposed @onlinecop without char count $regex = '^(?=(?:[0-9a-za-z\s"]|<br>)+$).+$' /* * first: has valid length? * second: has valid characters/tags? */ if (count(strip_tags($userstring)) <= $maxlength) && preg_match($regex, $userstring)) { echo 'ok'; }
php regex
No comments:
Post a Comment