到目前为止,我有这样的想法:
preg_replace("/[^a-zA-Z0-9\/!?\" \' :,.;><_ ]/", "",
html_entity_decode($text, ENT_QUOTES));它运行良好,如果我使用其他字符串的一部分,从链接.How我接受<script></script> <iframe> <a href=""></a> http:// https://吗?
发布于 2016-03-21 20:47:18
我过去用RegEx做过很多项目,下面是我的一些疑问。
匹配页面上的“每个”链接。
$links = preg_match_all('#(?:<a\s+.*?href=[\'"]([^\'"]+)[\'"]\s*?.*?>((?:\s*(?!<\s*\/\s*a\s*>).\s*)*)<\s*\/\s*a\s*>)#i',$html,$patterns);
// $patterns[0] (array) will give you the full tag <a herf="" ...etc
// $patterns[1] (array) will give you the urls您应该print_r($patterns)以确保实际的数组看起来是什么样子,以及您希望如何使用它们。
To match <script> tags (这实际上会找到完整的javascript块,这可能不是你想要的),但是你可以修改代码。
preg_match_all("#<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>#i",$html,$scripts); 匹配<iframe>你可以使用这个函数(匹配中的“每个”标签)
function html_iframe_tags($str)
{
$iframes = array();
$iframeSearch = preg_match_all('#(?:<iframe[^>]*)(?:(?:/>)|(?:>.*?</\s*iframe>))#i', $str, $rawiframes);
if (count($rawiframes[0])<1) return false;
for ($i = 0; $i < count($rawiframes[0]); $i++)
{
$iframes[$i]['tag'] = $rawiframes[0][$i];
preg_match_all('/src="([^"]*)"/i',$iframes[$i]['tag'], $iframesrc);
$iframes[$i]['src'] = (isset($iframesrc[1][0]) ? $iframesrc[1][0] : '');
preg_match_all('/\swidth="([^"]*)"/i',$iframes[$i]['tag'], $iframewidth);
$iframes[$i]['width'] = (isset($iframewidth[1][0]) ? $iframewidth[1][0] : '');
preg_match_all('/\sheight="([^"]*)"/i',$iframes[$i]['tag'], $iframeheight);
$iframes[$i]['height'] = (isset($iframeheight[1][0]) ? $iframeheight[1][0] : '');
}
return $iframes;
}然后print_r()结果并查看数组在实际使用中的效果,该函数实际上确定的不仅仅是您的使用,如宽度/高度等。而且还包括您正在查找的src。
希望这篇文章能为你的项目指明方向。
下面是一个在html中引用regex的网站。
http://www.the-art-of-web.com/php/parse-links/
https://stackoverflow.com/questions/36131008
复制相似问题