st4ck0v3rfl0w
st4ck0v3rfl0w

Reputation: 6755

Parse HTML and isolate the integer found after a known prefix of qualifying tags with id attributes

Simply put,

I have a string with a prefix "msg" followed by some numbers that serve as the ID for a list item

e.g.

<li id="msg1"></li>..............<li id="msg1234567890"></li>

What is the most efficient way to grab just the numbers?

In VB, I'd do the following:

str = "msg1"
str = right(str,len(str)-3)

How would I do something similar (or more efficient) in PHP?

Upvotes: 1

Views: 158

Answers (4)

mickmackusa
mickmackusa

Reputation: 47854

When parsing valid HTML, use an HTML parser.

Below demonstrates how to use DomDocument and an XPath query to specifically target li elements with msg prefixed id values, then uses sscanf() to isolate the integer after msg (cast as an integer) before being pushed into the result array.

Code: (Demo)

$html = <<<HTML
<ul>
    <li id="msg1"></li>
    <li id="msg1234567890"></li>
</ul>
HTML;

$dom = new DomDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$result = [];
foreach ($xpath->evaluate("//li[starts-with(@id, 'msg')]/@id") as $id) {
    sscanf($id->nodeValue, 'msg%d', $result[]);
}
var_export($result);

Output:

array (
  0 => 1,
  1 => 1234567890,
)

Upvotes: 0

Homer6
Homer6

Reputation: 15159

substr( $string, 3 );

See https://www.php.net/manual/en/function.substr.php

Upvotes: 0

oezi
oezi

Reputation: 51797

the same in php (using substr):

$str = "msg1";
$str = substr($str,3);

Upvotes: 3

Blizz
Blizz

Reputation: 8400

Just use preg:

preg_match_all('%<li id="msg(\d+)"></li>%i', $subject, $result, PREG_PATTERN_ORDER);

Upvotes: 0

Related Questions