正则表达式不返回任何与所使用的表达式无关的匹配项 [英] Regex not returning any matches independent of expression used

查看:217
本文介绍了正则表达式不返回任何与所使用的表达式无关的匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,这是问题所在,我一直在尝试构建cURL脚本来检查数据库中的无效链接.链接都看起来像这样的http://www.ltblekinge.se/download/18.9c16a31109c04a3e880003750.我遇到的问题是,我使用$url_list的正则表达式模式"没有任何内容.任何帮助将不胜感激!

Ok here is the issue, I have been trying to build cURL script to check for dead links in a database. The links all look something like this http://www.ltblekinge.se/download/18.9c16a31109c04a3e880003750. The issue that I have is that no mater what regex "pattern" I use the $url_list remains empty. Any help would be appreciated!

代码的问题部分

<?php
/*Config*/
/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = 'root';
/*curl setup of varibles*/
$excluded_domains = array(  
'localhost', 'rollnstroll.se');
$max_connections = 10;
$url_list = array();  
$working_urls = array();  
$dead_urls = array();  
$not_found_urls = array();  
$active = null;



try {
$dbh = new PDO("mysql:host=$hostname;dbname=blankett", $username, $password);
$dbh->exec('SET CHARACTER SET utf8');

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


/*** fetch into an PDOStatement object ***/
$sql = "SELECT * FROM `forms2`";

$stmt = $dbh->prepare("SELECT * FROM forms2");
$stmt->execute();

while ($d = $stmt->fetchAll()) {

    if (preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/', $d['link_forms'], $matches)) {

/***error code***/
if (preg_last_error() == PREG_NO_ERROR) {
print 'There is no error.';
}
else if (preg_last_error() == PREG_INTERNAL_ERROR) {
print 'There is an internal error!';
}
else if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
print 'Backtrack limit was exhausted!';
}
else if (preg_last_error() == PREG_RECURSION_LIMIT_ERROR) {
print 'Recursion limit was exhausted!';
}
else if (preg_last_error() == PREG_BAD_UTF8_ERROR) {
print 'Bad UTF8 error!';
}
else if (preg_last_error() == PREG_BAD_UTF8_ERROR) {
print 'Bad UTF8 offset error!';
}

    foreach ($matches[1] as $url) { 



        // exclude some domains  
        $tmp = parse_url($url);  
        if (in_array($tmp['host'], $excluded_domains)) {  
            continue;  
        }
        // store the url  
        $url_list []= $url; 
    }
   }
}

// remove duplicates  
$url_list = array_values(array_unique($url_list));

if (!$url_list) {  
die('No URL to check');  
}  


}
catch(PDOException $e)
{
echo $e->getMessage();
}

数据库结构

1 id    int(10) No  None    AUTO_INCREMENT
2 master_id     int(10) No  None
3   name_form   varchar(500) latin1_swedish_ci No   None
4   link_form   varchar(500)    latin1_swedish_ci No    None
5   date_added  timestamp   No  CURRENT_TIMESTAMP

问题 为什么$url_list为空?

推荐答案

这对我有用:

$url="http://www.ltblekinge.se/download/18.9c16a31109c04a3e880003750 http://one.com www.two.com http://yourad.io";

preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/', $url, $matches);

print_r($matches[1]);

输出:

Array
(
    [0] => http://www.ltblekinge.se/download/18.9c16a31109c04a3e880003750
    [1] => http://one.com
    [2] => www.two.com
    [3] => http://yourad.io
)

检查您的$d['link_forms']

这篇关于正则表达式不返回任何与所使用的表达式无关的匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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