Is there a way to grab all elements with an id that partially match. For example, if I want to grab all HTML elements on the webpage with an id attribute that starts with msg_ but could be anything after that.
Here's what I go so far:
$doc = new DomDocument;
// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('{URL IS HERE}'));
foreach($doc->getElementById('msg_') as $element) {
foreach($element->getElementsByTagName('a') as $link)
{
echo $link->nodeValue . "\n";
}
}
But I need to figure out how to do a partial id match with this bit: $doc->getElementById('msg_') or if there's some other way to accomplish this...??
Basically, I need to grab all 'a' tags that are children of the element with the id starting with msg_ Technically there's always, only, going to be 1 a tag, but I don't know how to grab just the first Child, which is why I'm using a foreach on this also.
Is this possible with the DomDocument PHP Class?
Here is the code I'm using now, which doesn't work either:
$str = '';
$filename = 'http://dream-portal.net/index.php/board,65.0.html';
@set_time_limit(0);
$fp = fopen($filename, 'rb');
while (!feof($fp))
{
$str .= fgets($fp, 16384);
}
fclose($fp);
$doc = new DOMDocument();
$doc->loadXML($str);
$selector = new DOMXPath($doc);
$elements = $selector->query('//row[starts-with(@id, "msg_")]');
foreach ($elements as $node) {
var_dump($node->nodeValue) . PHP_EOL;
}
HTML is as follows (it's in the span tag):
<td class="subject windowbg2">
<div>
<span id="msg_6555">
<a href="http://dream-portal.net/index.php?topic=834.0">Poll 1.0</a>
</span>
<p>
Started by
<a href="http://dream-portal.net/index.php?action=profile;u=1" title="View the profile of SoLoGHoST">SoLoGHoST</a>
<small id="pages6555">
«
<a class="navPages" href="http://dream-portal.net/index.php?topic=834.0">1</a>
<a class="navPages" href="http://dream-portal.net/index.php?topic=834.15">2</a>
»
</small>
with 963 Views
</p>
</div>
</td>
It's the <span id="msg_ part, and there are a bunch of these (atleast 15 on the HTML page).
Use this:
$str = file_get_contents('http://dream-portal.net/index.php/board,65.0.html');
$doc = new DOMDocument();
@$doc->loadHTML($str);
$selector = new DOMXPath($doc);
foreach ($selector->query('//*[starts-with(@id, "msg_")]') as $node) {
var_dump($node->nodeValue) . PHP_EOL;
}
Gives you:
string(8) "Poll 1.0"
string(12) "Shoutbox 2.2"
string(24) "Polaroid Attachments 1.6"
string(24) "Featured News Slider 1.3"
string(17) "Image Resizer 1.0"
string(8) "Blog 2.2"
string(13) "RSS Feeds 1.0"
string(19) "Adspace Manager 1.2"
string(21) "Facebook Like Box 1.0"
string(15) "Price Table 1.0"
string(13) "SMF Links 1.0"
string(19) "Download System 1.2"
string(16) "[*]Site News 1.0"
string(12) "Calendar 1.3"
string(16) "Page Peel Ad 1.1"
string(20) "Sexy Bookmarks 1.0.1"
string(15) "Forum Staff 1.2"
string(21) "Facebook Comments 1.0"
string(15) "Attachments 1.4"
string(25) "YouTube Channels 0.9 Beta"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With