regex - PHP regular expression -
what purpose of following code?
preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
what kind of $string match expression?
why there character @?
that regular expression match <script>...</style>
or <style>...</style>
(x)html blocks in string , remove them. done prevent users inserting these (potentially harmful) tags data might echo user. if not removed, allow malicious users change site appearance, or insert javascript site rewrites page content; might force users visit other websites automatically , many other nasty things.
as @
.... when defining regular expressions, traditionally enclosed slash example:
/regexphere/si
the /
around regular expression indicates boundaries , characters trailing second slash there flags regular expression engine behave way. in particular i
means "case insensitive" , s
means .
in expression should match whitespace newlines , tabs. format inherited php perl , other unix utilities predate it.
other characters (like @
or |
or %
) can used replace /
around regular expression though avoid unnecessary escaping when there lot of /
s in pattern. example, it's easier , more readable write @http://@
/http:\/\//
. in pattern makes easier not escape /
in closing tag.
Comments
Post a Comment