Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用正则表达式获取值?_Php_Regex - Fatal编程技术网

Php 如何使用正则表达式获取值?

Php 如何使用正则表达式获取值?,php,regex,Php,Regex,您好,我的正则表达式代码有一个问题,我使用PHP从HTML标记中获取一个值。我有以下可能的字符串: <span class="down last_position">xyz</span> <span class="up last_position">xyz</span> <span class="last_position new">xyz</span> xyz xyz xyz 我有以下preg_match命令: pre

您好,我的正则表达式代码有一个问题,我使用PHP从HTML标记中获取一个值。我有以下可能的字符串:

<span class="down last_position">xyz</span>
<span class="up last_position">xyz</span>
<span class="last_position new">xyz</span>
xyz
xyz
xyz
我有以下preg_match命令:

preg_match('#<span class="last_position.*?">(.+)</span>#', $string, $matches);
preg#u match('#(.+)#',$string,$matches);
这几乎涵盖了案例3。所以我想知道我需要在最后一个职位前添加什么,才能让所有的案例都成为可能

非常感谢


编辑:对于所有想知道要匹配什么值的人:“xyz”

避免使用正则表达式解析HTML,因为它很容易出错。使用DOM解析器可以更好地解决您的特定用例:

$html = <<< EOF
<span class="down last_position">xyz</span>
<span class="up last_position">xyz</span>
<span class="last_position new">xyz</span>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$nodeList = $xpath->query("//span[contains(@class, 'last_position')]/text()");
for($i=0; $i < $nodeList->length; $i++) {
    $node = $nodeList->item($i);
    var_dump($node->nodeValue);
}
试着用这个

preg_match('#<span class="?(.*)last_position.*?">(.+)</span>#', $string, $matches);
preg#u match('#(.+)#',$string,$matches);
您可以尝试以下方法:

preg_match_all('#<span class="[^"]*last_position[^"]*">(.+)</span>#', $string, $matches, PREG_PATTERN_ORDER);
preg#u match_all('.+)#',$string,$matches,preg#u PATTERN_ORDER);
然后,您将在
$matches[1][0]
$matches[1][1]
$matches[1][2]
中找到这些值

在类属性值
[^”]*
中添加的第一部分匹配与双引号不匹配的任意数量的字符。因此它匹配属性值中的任何内容。

尝试以下操作(是的,您可以使用正则表达式匹配HTML中的数据):

$string='xyz
xyz
xyz′;
preg_match_all('#(.+)#i',$string,$m);
印刷费(百万美元);

当然,使用正则表达式解析XML是不可能的,因为XML是不规则的。但在许多实际情况下,用作输入的XML文档是有限的,并且可以预测,因此只能作为文本处理

像这样的东西应该适合你:

preg_match('#<span class="[^>"]*?last_position[^>"]*">(.+)</span>#', $string, $matches);
preg#u match('#“]*”>(.+)#',$string,$matches);

不要使用正则表达式来解析HTML。虽然在某些情况下可以使用正则表达式,但最好使用HTML解析器来完成您正在执行的任务。是的,我知道,我正在使用DomDocument进行整个解析。我只是想知道是否有人会知道……请注意,如果一堆span标记在同一行上,这将不起作用。@nhahtdh这不是在问题中提到。这取决于你想做多少假设。目前,它将很容易因新线的变化而中断。请解释模式。为什么这样做有效?关于性能,使用Dom或preg_match__all的最佳方法是什么?@ElSinus:人们也抱怨正则表达式的性能。所以我确定使用哪种方法ne会更快。我接受了这个答案,因为我的问题不是如何使用DomDocument,而是如何使用Regex。非常详细,谢谢:)请解释模式。为什么这样做?@HamZaDzCyberDeV:
几乎所有情况
如何量化“几乎所有情况”"? 我不能对我的投票做任何事,因为它被锁定了,但我认为这个答案不值得投票。@nhahdh好的,你让我到了那里,我不会争辩,因为你首先反对将正则表达式用于HTML。请解释模式。为什么这样做有效?
$string = '<span class="down last_position">xyz</span>
<span class="up last_position">xyz</span>
<span class="last_position new">xyz</span>';

preg_match_all('#<span\s.*?class=".*?last_position.*?".*?>(.+?)</span>#i', $string, $m);
print_r($m);
preg_match('#<span class="[^>"]*?last_position[^>"]*">(.+)</span>#', $string, $matches);