我想从字符串/story.php?title=googlegoogle-google-google
中获取$cool
。获得它的唯一方法是使用多行正则表达式。我尝试了下面的代码,但它没有用。任何人都能帮助我吗?
<?php
$cool ='<li><span class="sidebar-vote-number"><a href="/story.php?title=gamestarts now">1</a></span><span class="sidebar-article"><a href="/story.php?title=googlegoogle-google-google" class="switchurl">GoogleGoogle Google Google</a></span></li>
Published News</a></div>
</div>
<div class="boxcontent">
<ul class="sidebar-stories">
<li><span class="sidebar-vote-number"><a href="/story.php?title=googlegoogle-google-google">1</a></span><span class="sidebar-article"><a href="/story.php?title=googlegoogle-google-google" class="switchurl">GoogleGoogle Google Google</a></span></li>';
preg_match('/Published News<\/a><\/div>
<\/div>
<div class="boxcontent">
<ul class="sidebar-stories">
<li><span class="sidebar-vote-number"><a href="(.*?)"/msU', $cool, $match);
echo $match[1];
?>
答案 0 :(得分:3)
有什么问题
preg_match('~Published News.*<li><span class="sidebar-vote-number"><a href="([^"]*)~ms', $cool, $match);
?确保使用正确的变量$cool
!
你知道什么?哪种模式?
答案 1 :(得分:0)
“获得它的唯一方法是使用多行正则表达式。”哦不,不是。
除此之外,您将数据分配给名为$ cool的变量,但是您将一个名为$ game的变量传递给preg_match()函数。
答案 2 :(得分:0)
preg_match('/Published News<\/a><\/div>
<\/div>
<div class="boxcontent">
<ul class="sidebar-stories">
<li><span class="sidebar-vote-number"><a href="(.*)">/msU', $cool, $match);
它有效。
答案 3 :(得分:0)
如果您的HTML字符串是半良好的,您可以使用Xpath。 http://codepad.org/vVBFNY7r
<?php
$cool ='<ul><li>
<span class="sidebar-vote-number"><a href="/story.php?title=gamestarts now">1</a></span>
<span class="sidebar-article"><a href="/story.php?title=googlegoogle-google-google" class="switchurl">GoogleGoogle Google Google</a></span>
</li></ul>';
$dom = new DOMDocument();
$dom->loadHTML($cool);
$xpath = new DOMXPath($dom);
$elements= $xpath->query('//a/@href');
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
输出:
<br/>[href]/story.php?title=gamestarts now
<br/>[href]/story.php?title=googlegoogle-google-google