简体   繁体   中英

Regular expression to match an specific word in php

I have struck in the string preg match in php. From the below string i need to match 'index.php?c_id=' and need to get the value of that string. (Ex: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

Thanks & regards Kaif

In primis using regexes to parse HTML is generally a bad idea. 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

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.

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)

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. Instead, DomDocument and Xpath can do that work

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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