preg_match不匹配

时间:2013-04-22 08:10:11

标签: php curl preg-match

我一直试图用php preg_match解析这个网站http://www.nzz.ch/aktuell/zuerich/stadt_region/notrufdienst-am-handgelenk-1.18068637的源代码几天没有成功。卷曲和搜索标题的阅读也很顺利。

在使用preg_match搜索描述时出现问题。 preg_match('/Description" content="(.*)">/i', $content, $description); $ogdescription = substr($description[0], 22, -1);

此搜索的结果是:

Ein Telefon nützt wenig, wenn es sich im Notfall nicht in greifbarer Nähe befindet. Das haben sich die Gründer der Firma Limmex gedacht und eine Armbanduhr auf den Markt gebracht, die per Knopfdruck zum Mobiltelefon wird." /&gt;<meta name="Publisher" content="Neue Zürcher Zeitung AG, Switzerland"><meta name="email" content="online.redaktion@nzz.ch"><meta name="copyright" content="Copyright &amp;copy; 2013 Neue Zürcher Zeitung AG, Switzerland, http://www.nzz.ch/agb"><meta http-equiv="content-language" content="de"><meta http-equiv="expires" content="-1"><meta name="Author" content="Impressum Neue Zürcher Zeitung, Switzerland, http://www.nzz.ch/impressum"><meta name="distribution" content="global"><meta name="robots" content="index, follow, noarchive, noodp"><meta name="revisit-after" content="1 hour"><meta http-equiv="pragma" content="no-cache"><meta property="fb:app_id" content="138008459595436"><meta property="og:title" content="Zürcher Jungunternehmen: Notrufdienst am Handgelenk - Übersicht Nachrichten - NZZ.ch"><meta property="og:type" content="article"><meta property="og:url" content="http://www.nzz.ch/aktuell/zuerich/stadt_region/notrufdienst-am-handgelenk-1.18068637"><meta property="og:image" content="http://images.nzz.ch/app.php/eos/v2/image/view/145/-/lead/25b76387/1.18068643.1366564054.jpg"><meta property="og:site_name" content="Neue Zürcher Zeitung"><meta name="viewport" content="width=1390"><meta name="application-name" content="NZZ Online"><meta name="msapplication-tooltip" content="Start NZZ Online in Site Mode"><meta name="msapplication-starturl" content="http://www.nzz.ch"><meta name="msapplication-window" content="width=1024;height=760"><meta name="msapplication-navbutton-color" content="#000000"><meta name="msapplication-task" content="name=Nachrichten; action-uri=http://www.nzz.ch/nachrichten; icon-uri=http://www.nzz.ch/version-2.8.1/bundles/website/img/favicon.ico"><meta name="msapplication-task" content="name=Finanzen; action-uri=http://www.nzz.ch/finanzen; icon-uri=http://www.nzz.ch/version-2.8.1/bundles/website/img/favicon.ico"><meta name="msapplication-task" content="name=Magazin; action-uri=http://www.nzz.ch/magazin; icon-uri=http://www.nzz.ch/version-2.8.1/bundles/website/img/favicon.ico"><meta name="msapplication-task" content="name=Marktplätze; action-uri=http://www.nzz.ch/marktplaetze; icon-uri=http://www.nzz.ch/version-2.8.1/bundles/website/img/favicon.ico"><link href="http://www.nzz.ch/version-2.8.1/bundles/website/img/favicon.ico" rel="icon" type="image/x-icon"><link rel="shortcut icon" href="http://www.nzz.ch/version-2.8.1/bundles/website/img/favicon.ico"><link rel="apple-touch-icon" href="http://www.nzz.ch/version-2.8.1/bundles/website/img/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="72x72" href="http://www.nzz.ch/version-2.8.1/bundles/website/img/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="114x114" href="http://www.nzz.ch/version-2.8.1/bundles/website/img/apple-touch-icon-114x114.png"><link rel="publisher" href="https://plus.google.com/118094012157733055567"><link rel="stylesheet" type="text/css" media="screen, projection, handheld, print" href="/bundles/website/css/jquery.fancybox-1.3.4.css"><title>Zürcher Jungunternehmen: Notrufdienst am Handgelenk - Übersicht Nachrichten - NZZ.ch</title><link rel="canonical" href="http://www.nzz.ch/aktuell/zuerich/stadt_region/notrufdienst-am-handgelenk-1.18068637"><link rel="stylesheet" type="text/css" href="/version-2.8.1/css/6927630.css"><link rel="stylesheet" type="text/css" media="print" href="/version-2.8.1/css/5bd0678.css"><meta http-equiv="imagetoolbar" content="false"><link rel="search" href="/search" title="Suche Neue Zürcher Zeitung, Switzerland, http://www.nzz.ch/search"><link rel="author" href="/impressum" title="Impressum Neue Zürcher Zeitung, Switzerland, http://www.nzz.ch/impressum"><link rel="copyright" href="/agb" title="Copyright &amp;copy; 2013 Neue Zürcher Zeitung AG, Switzerland, http://www.nzz.ch/agb"><!--[if lt IE 9]><script type="text/javascript" src="/version-2.8.1/bundles/website/js/html5shiv.js"></script><![endif]--><!-- Maxymiser script start --><!-- Maxymiser script end -->

我做错了什么,它将所有源代码都包含在内?


下一个问题:

preg_match('/escription" content="(.*?)" \/>/i', $content, $description);

$ ogdescription = $ description [1];

<meta name="twitter:description" content="Am 17 .4. 2013 besch&auml;ftigte sich das &Ouml;1-Gesundheitsmagazin mit Pers&ouml;nlicher Assistenz und dem Recht auf Selbstbestimmung.">

为什么不匹配?

2 个答案:

答案 0 :(得分:0)

preg_match('/Description" content="(.*?)" \/>/i', $content, $description); 

让您的搜索“非贪婪”注意'?'添加到你的正则表达式

没有?正则表达式将匹配最后一个&gt;它找到的符号。随着?比赛结束后第一场比赛将停止。

您还需要转义的正斜杠和空格,因为内容是xHTML。

使用此正则表达式

$description[0] // will contain the full matched tag
$description[1] // will contain just the text from inside the tag

答案 1 :(得分:0)

preg_match('/Description" content="(.*)"/i', $content, $description);   
$ogdescription = substr($description[0], 22, -1);

无需匹配&gt;

preg_match('/Description" content="(.*?)" \/>/i', $a, $description); 
print_r($description); // use $description[1]

Array
(
    [0] => Description" content="Ein Telefon nützt wenig, wenn es sich im Notfall nicht in greifbarer Nähe befindet. Das haben sich die Gründer der Firma Limmex gedacht und eine Armbanduhr auf den Markt gebracht, die per Knopfdruck zum Mobiltelefon wird." />
    [1] => Ein Telefon nützt wenig, wenn es sich im Notfall nicht in greifbarer Nähe befindet. Das haben sich die Gründer der Firma Limmex gedacht und eine Armbanduhr auf den Markt gebracht, die per Knopfdruck zum Mobiltelefon wird.
)