简体   繁体   English

允许链接与preg-replace

[英]Allow links with preg-replace

I have this so far : 到目前为止,我有:

preg_replace("/[^a-zA-Z0-9\/!?\" \' :,.;><_ ]/", "", 
html_entity_decode($text, ENT_QUOTES));

It works well, if I use other string a part from links .How do i accept <script></script> <iframe> <a href=""></a> http:// https:// ? 如果我在链接中使用其他字符串作为一部分,效果很好。如何接受<script></script> <iframe> <a href=""></a> http:// https://

I have done many projects with RegEx in the past, here is a few of my queries. 过去,我曾使用RegEx进行过许多项目,以下是我的一些查询。

Match "Every" link on a page. 匹配页面上的“每个”链接。

$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

You should print_r($patterns) to be sure what the actual arrays look like and how you want to use them. 您应该使用print_r($patterns)来确定实际的数组是什么样,以及如何使用它们。

To match <script> tags (this actually finds full javascript blocks, which may not be exactly what you're asking), however you can modify the code some. 要匹配<script>标签 (实际上是找到完整的javascript块,可能与您要求的不完全相同),但是您可以对代码进行一些修改。

preg_match_all("#<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>#i",$html,$scripts); 

To match <iframe> you can use this function (matches "every" iframe tag within html) 要匹配<iframe>您可以使用此功能(匹配html中的“每个” 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;
 }

Then print_r() the results and see how the array looks for your exact usage, this function actually determines more than your use such as width/height etc. But also includes the src of which you are looking for. 然后使用print_r()结果,并查看数组如何查找您的确切用法,该函数实际上确定的不仅仅是您的用途,例如width / height等。而且还包括您要查找的src

Hopefully this stuff can give you direction for your project. 希望这些东西可以为您的项目指明方向。

Here is a website that has some reference to regex in html 这是一个在html中对regex有参考的网站
http://www.the-art-of-web.com/php/parse-links/ http://www.the-art-of-web.com/php/parse-links/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM