简体   繁体   English

正则表达式以匹配php中的特定单词

[英]Regular expression to match an specific word in php

I have struck in the string preg match in php. 我在php中进行了字符串preg匹配。 From the below string i need to match 'index.php?c_id=' and need to get the value of that string. 从下面的字符串中,我需要匹配“ index.php?c_id =”,并需要获取该字符串的值。 (Ex:index.php?c_id=161377) (例如:index.php的C_ID = 161377?)

$str = '<h3>Resources</h3>
<p><a href="index.php?ci_id=161377">Announcing Upgraded Firmware for N3680 Decoded 2D Imager</a></p>
<p><a href="https://www.honeywellaidc.com/products/oem-scan-engines/2d-imagers/n3680-series">N3680 Product webpage</a></p>
<p><a href="index.php?ci_id=161376">N3680 Product datasheet</a></p>';
preg_match_all('#index.php?([^\s]+)"#', $str, $matches,PREG_OFFSET_CAPTURE);
print_r($matches[1]);

I need the output: 161377 161376 我需要输出:161377 161376

Thanks & regards Kaif 谢谢和问候凯夫

In primis using regexes to parse HTML is generally a bad idea. 在最初,使用正则表达式解析HTML通常是一个坏主意。 It works here just because you are not trying anything more complex than finding a word, but avoid this tactic in the future, or you will end up trying to do something which cannot be done. 它之所以在这里起作用,只是因为您没有尝试比找到单词更复杂的事情,但是将来避免使用这种策略,否则您最终将尝试做一些无法完成的事情。

Beside the warning, you are simply looking in the wrong place. 除了警告,您只是在错误的地方寻找。 preg_match's documentation says preg_match的文档

If matches is provided, then it is filled with the results of search. 如果提供了匹配项,则将其填充为搜索结果。 $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. $matches[0]将包含与完整模式匹配的文本, $matches[1]将具有与第一个捕获的带括号的子模式$matches[1]的文本,依此类推。

So to find all the matches, you'll simply have to look in $matches[0] instead of $matches[1] (or to look in all the positions of $matches from 1 on) 因此,要查找所有匹配项,您只需查看$matches[0]而不是$matches[1] (或从1开始查看$matches所有位置)

Thanks you guys, for your support. 谢谢你们的支持。 Based on your comments i found the answer. 根据您的评论,我找到了答案。

$str = '<h3>Resources</h3>
<p><a href="index.php?ci_id=161377">Announcing Upgraded Firmware for N3680 Decoded 2D Imager</a></p>
<p><a href="https://www.honeywellaidc.com/products/oem-scan-engines/2d-imagers/n3680-series">N3680 Product webpage</a></p>
<p><a href="index.php?ci_id=161376">N3680 Product datasheet</a></p>';
preg_match_all('/index\.php\?ci_id=([0-9]+)/', $str, $matches,PREG_OFFSET_CAPTURE);
$i=0;
foreach($matches[1] as $key => $val)
{
    echo '<br>'.$val[$i];
}

Don't use regex to parse html. 不要使用正则表达式来解析html。 Instead, DomDocument and Xpath can do that work 相反,DomDocument和Xpath可以做到这一点

$dom = new DomDocument();
$dom->loadHTML($str);

$xpath = new DomXpath($dom);
$hrefs = $xpath->evaluate('//a[starts-with(@href, "index.php?ci_id")]/@href');
foreach($hrefs as $href) {
  list(, $ci_id) =  explode('=', $href->nodeValue);
  echo $ci_id ."<br>\n";
}

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM