PHP从数组中删除非URL [英] PHP Remove Non-URLs From Array

查看:46
本文介绍了PHP从数组中删除非URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP的正则表达式从网站中抓取URL列表,并使用以下代码来清理结果.

I am scraping a list of URLs from a website with regex in PHP and use the following code to clean up the results.

// Echo each found result
foreach ($matches as $val) {
    $results[] = GetStringBetween($val[1], "http://", "&");
}

$results = array_unique($results);
$results = array_filter($results);

它可以工作,但是问题是它会产生一些不需要的结果,例如:

It works however the problem is it gives some unwanted results like:

ed_search?q=product

有什么方法可以过滤并仅将有效网址保留在数组中?

Is there any way to filter and keep only valid urls in an array?

我需要URL以www.,http:,https:

I need the URLs to start with either www., http:, https:

推荐答案

$url = "xxxxx";

if( filter_var($url, FILTER_VALIDATE_URL)  !== FALSE )
{
    // validated as an url
}

http://php.net/manual/en/filter.filters.validate.php

这篇关于PHP从数组中删除非URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆