简体   繁体   English

如何使用 mailto 避免 url:

[英]How to avoid url with mailto:

I'm working in php and I have created a function that is getting links from a submitted url.我在 php 工作,我创建了一个 function,它从提交的 url 获取链接。 The code is working fine, but it is picking even links that are not active like mailto:, , javascript:void(0).该代码运行良好,但它甚至可以选择不活动的链接,例如 mailto:, , javascript:void(0)。 How can I avoid picking up a tags whose href are like: href="mailto:";我怎样才能避免拿起一个标签,其href类似于:href="mailto:"; href="tel:"; href="电话:"; href="javascript:"? href="javascript:"? Thanks you in advance.提前谢谢你。

function check_all_links($url) {
$doc = new DOMDocument();
@$doc->loadHTML(file_get_contents($url));
$linklist = $doc->getElementsByTagName("a");
$title = $doc->getElementsByTagName("title");
$href = array();
$page_url = $full_url = $new_url = "";
$full_url = goodUrl($url);
$scheme = parse_url($url, PHP_URL_SCHEME);

$slash = '/';
$links = array();
$linkNo = array();

if ($scheme == "http") {
    
    foreach ($linklist as $link) {
        $href = strtolower($link->getAttribute('href'));
        $page_url = parse_url($href, PHP_URL_PATH);
        $new_url = $scheme."://".$full_url.'/'.ltrim($page_url, '/');
        //check if href has mailto: or # or javascipt() or tel:
          
        if (strpos($page_url, "tel:") === True) { 
            continue;
        } 
    

        if(!in_array($new_url, $linkNo)) {
           echo $new_url."<br>" ;
           array_push($linkNo, $new_url);
           $links[]  = array('Links' => $new_url );
        }
    }
}else if ($scheme == "https") {
    
    foreach ($linklist as $link) {
        $href = strtolower($link->getAttribute('href'));
        $page_url = parse_url($href, PHP_URL_PATH);
        $new_url = $scheme."://".$full_url.'/'.ltrim($page_url, '/'); 

        if (strpos($page_url, "tel:") === True) { 
            continue; 
        } 
        
        if(!in_array($new_url, $linkNo)) {
           echo $new_url."<br>" ;
           array_push($linkNo, $new_url);
           $links[]  = array('Links' => $new_url );
        }
    }
}

You can use the scheme field from the parse_url function result.您可以使用parse_url function 结果中的scheme字段。 Instead of:代替:

if (strpos($page_url, "tel:") === True) { 
        continue;
} 

you can use:您可以使用:

if (isset($page_url["scheme"] && in_array($page_url["scheme"], ["mailto", "tel", "javascript"]) {
    continue;
}

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

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