Php 多行正则表达式不工作
我试图从字符串Php 多行正则表达式不工作,php,regex,Php,Regex,我试图从字符串$cool中获取/story.php?title=google。获得它的唯一方法是使用多行正则表达式。我尝试了下面的代码,但没有成功。有人能帮我吗 <?php $cool ='<li><span class="sidebar-vote-number"><a href="/story.php?title=gamestarts now">1</a></span><span class="sidebar-
$cool
中获取/story.php?title=google
。获得它的唯一方法是使用多行正则表达式。我尝试了下面的代码,但没有成功。有人能帮我吗
<?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];
?>
“获得它的唯一方法是使用多行正则表达式。”哦,不,不是
除此之外,您正在将数据分配给一个名为$cool的变量,但您正在将一个名为$game的变量传递给preg_match()函数。“获取该变量的唯一方法是使用多行正则表达式。”哦,不,它不是
除此之外,您正在将数据分配给一个名为$cool的变量,但您正在将一个名为$game的变量传递给preg_match()函数。有什么问题吗
preg_match('~Published News.*<li><span class="sidebar-vote-number"><a href="([^"]*)~ms', $cool, $match);
preg_match(“~已发布新闻。*怎么了
preg_match('~Published News.*<li><span class="sidebar-vote-number"><a href="([^"]*)~ms', $cool, $match);
preg\u match('~已发布新闻。*preg\u match('/已发布新闻
- /msU',$cool,$match);
它可以工作。preg_match('/Published News
- /msU',$cool,$match);
它可以工作。如果您的HTML字符串是半格式的,您可以使用Xpath
如果HTML字符串是半格式的,则可以使用Xpath
别无选择,有多种类似的语法…我只知道regex..还有什么可以帮助它呢?不,我想我已经通过在末尾添加msU实现了使用类似regex的多行html..但这段代码似乎不太合适work@Antony-当人们提到那篇文章时,这真的很烦人。这不是一个答案,这是一个咆哮,而且它只会持续存在“历史价值”的成因.You,但不推荐使用。PHP特别有一个非常糟糕的内置HTML解析器,但有些解析器工作得很好。没有其他方法,有多种类似的语法…我只知道正则表达式。还有什么可以帮助它呢?不,我想我已经通过在最后添加msU实现了使用类似正则表达式的多行HTML…但这段代码似乎不起作用k@Antony-当人们引用那篇文章时,这真的很烦人。这不是一个答案,这是一个咆哮,它只是因为“历史价值”而存在。你知道,但不推荐。PHP特别有一个非常糟糕的内置HTML解析器,但有一些很好用。尼斯:)和/,只是想学习:)之间的区别是什么我相信只会找到第一个出现的链接。他的例子特别指出了第二种情况。不过,他是只想要第二个还是同时想要第一个和第二个还不完全清楚。@Brett我明白了,改变了我的模式。但也不确定是什么模式。@Vishnusinbu:没有太大区别。这些是分隔符。在/
上选择~
将不再需要转义/
。很好:)和/,只是想了解:)我相信这只会找到第一次出现的链接。他的例子特别指出了第二种情况。不过,他是只想要第二个还是同时想要第一个和第二个还不完全清楚。@Brett我明白了,改变了我的模式。但也不确定是什么模式。@Vishnusinbu:没有太大区别。这些是分隔符。在/
上选择~
将不再需要转义/
。它不会完全起作用。你应该知道,这完全行不通。你应该
<?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