我已经编写了一些代码来匹配和解析这种样式的Markdown链接:
[click to view a flower](http://www.yahoo.com/flower.html)
我有一个代码,旨在提取链接文本,然后提取url本身,并将它们粘贴到A HREF链接中。但我担心可能会有人通过注入XSS的方式来攻击,因为我留下了相当多的字符。这样做安全吗?
$pattern_square = '\[(.*?)\]';
$pattern_round = "\((.*?)\)";
$pattern = "/".$pattern_square.$pattern_round."/";
preg_match($pattern, $input, $matches);
$words = $matches[1];
$url = $matches[2];
$words = ereg_replace("[^-_@0-9a-zA-Z\.]", "", $words);
$url = ereg_replace("[^-A-Za-z0-9+&@#/%?=~_|!:.]","",$url);
$final = "<a href='$url'>$words</a>";
看起来它工作得不错,它确实排除了一些愚蠢的URL,包括分号和反斜杠,但我并不关心这些URL。