我知道我昨天问了一个关于正则表达式的问题(Regex not returning any matches independent of expression used)。只要我将所有URL:s手动放入输入字符串,我昨天得到的代码就可以正常工作,但是当我从数据库加载它时,我得到零匹配。
运行代码
$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 )
非功能代码
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'root';
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);
$stmt = $dbh->prepare("SELECT link_form FROM forms2");
$stmt->execute();
$url = $stmt->fetchAll(PDO::FETCH_ASSOC);
preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/', $url, $matches);
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!';
}
print_r($matches);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
给我输出:
There is no error.
这让我觉得我的输入字符串有问题,但我无法弄清楚它有什么问题。这就是我的输入字符串:
Array ( [0] => Array ( [link_form] => http://www.ltblekinge.se/download /18.9c16a31109c04a3e880003750/forskrivningsanvisn_rorelse_forflyttn ) [1] => Array ( [link_form] => http://www.ltdalarna.se/upload/vard_och_halsa/halso_och_sjukvarden/ldhjalpmedel/Hjalpmedelsguide/03Hjmhantering/Hantering%20av%20kostnader%20för%20hjälpmedel%20ver%202.pdf ) [2] => Array ( [link_form] => http://www.gotland.se/imcms/62373 ) [3] => Array ( [link_form] => http://www.hjalpnu.se/download/18.4fd9e13012a408581b980002702/bestallningsblankett.doc ) [4] => Array ( [link_form] => http://www.regionhalland.se/PageFiles/14881/Bil%20A%2013%20Förskrivning%20av%20hjälpmedel.pdf ) [5] => Array ( [link_form] => http://www.jll.se/download/18.5c5942f112d4af0bbaf80008216/Frågor+och+svar+-+brukare.pdf ) [6] => Array ( [link_form] => http://www.jll.se/download/18.5c5942f112d4af0bbaf80008218/Rådgivning+hjälpmedel.pdf ) [7] => Array ( [link_form] => http://www.jll.se/download/18.47d2594d12bccf68c3680001577/Definition+och+direktiv+egenansvar+och+hjä ) [8] => Array ( [link_form] => http://www.jll.se/download/18.2a18114134d5722c50800050/Fakturaunderlag+för+brukare+120112.pdf ) [9] => Array ( [link_form] => http://www.lj.se/info_files/infosida38484/lanevillkor_hjalpmedel.pdf ) [10] => Array ( [link_form] => http://www.lj.se/info_files/infosida38484/avvikelserapport.doc ) [11] => Array ( [link_form] => http://www.ltkronoberg.se/upload/Dokument/Halsa_och_vard/Hitta_ratt_i_varden/Funktionshinder/Hjalpmedel/Förskrivningsblankett.DOC ) [12] => Array ( [link_form] => https://www.nllplus.se/upload/IB/pv/hjpmk/blanketter/Särskild%20förskrivning.pdf ) [13] => Array ( [link_form] => https://www.nllplus.se/upload/IB/pv/hjpmk/Hjälpmedelsportalen/Lathund%20särskild%20förskrivning.doc ) [14] => Array ( [link_form] => http://www.skane.se/Public/HAB/Hjalpmedelshandbok/100-allmanna-riktlinjer.pdf ) [15] => Array ( [link_form] => http://www.slso.sll.se/upload/Hjalpmedel_Stockholm/Blanketter/Enstaka%20produkt%202012-12-04.doc ) [16] => Array ( [link_form] => http://www.liv.se/sidkat/4072/Regelverk%20OTA%20090205.pdf ) [17] => Array ( [link_form] => http://www.liv.se/sidkat/4072/Skrivelse%20gallande%20upphandling-05.doc ) [18] => Array ( [link_form] => http://www.liv.se/sidkat/4072/RemissOTA.pdf ) [19] => Array ( [link_form] => http://www.lvn.se/pagefiles/2548/fillistning/riktlinjer%20för%20förskrivning%20av%20hjälpmedel%20i%20landstinget%20västernorrland%20pdf.pdf ) [20] => Array ( [link_form] => http://www.lvn.se/pagefiles/12448/flödesschema%20ordnat%20införande%20produktgrupp_20111025.pdf ) [21] => Array ( [link_form] => http://www.lvn.se/PageFiles/6071/Blanketter/Beställningsblanketter/Beställning%20Medicintekniska%20hjälpmedel,%20120316.pdf ) [22] => Array ( [link_form] => http://www.lvn.se/PageFiles/6071/Blanketter/Beställningsblanketter/Bilaga%20B,%20hjälpmedel%20för%20stå%20och%20gå%20120313.pdf ) [23] => Array ( [link_form] => http://www.vgregion.se/sv/Omrade-Hjalpmedel/Har-du-en-ide-om-vad-vi-kan-gora-battre/Synpunkter-pa-Hjalpmedelscentralernas-sortiment/ ) [24] => Array ( [link_form] => http://www.vgregion.se/upload/Handbok%20för%20förskrivning%20av%20personliga%20hjälpmedel/Hjälpmedelsansökan%20Gbg%202011-02-22.doc ) [25] => Array ( [link_form] => http://www.orebroll.se/Files-sv/Örebro%20läns%20landsting/Vård%20och%20hälsa/Habilitering/Centrum%20för%20hjälpmedel/Förskrivarportal/Blanketter/Förskrivning%20av%20tekniska%20hjälpmedel.doc ) [26] => Array ( [link_form] => http://www.lio.se/Patientinformation/Hjalpmedel-och-handikappservice/Hjalpmedelsforskrivning/Forskrivningsratt-for-hjalpmedel/ ) [27] => Array ( [link_form] => http://www.lio.se/pages/37692/Behorighet%20bestallare%20landsting.pdf ) [28] => Array ( [link_form] => http://www.lio.se/pages/37692/behorighet%20Forskr%20landsting%2020090702.pdf ) ) There is no error.
问题 输入字符串有什么问题,为什么没有匹配?
答案 0 :(得分:2)
您的输入字符串不是字符串,而是数组。
foreach ($url as $u) {
preg_match_all(..., $u['link_form'], $matches);
}