I was wondering if there was any way to use dom to select elements that have dynamic tags. All of the tags start with link_(some id).
Example:
<tr id="link_111111">something in here...</tr>
<tr id="link_222222">something in here...</tr>
<tr id="link_333333">something in here...</tr>
<tr id="link_444444">something in here...</tr>
<tr id="link_555555">something in here...</tr>
I was wondering if I could grab all the tr's that have the id with link_ because I don't have the specific id's, they are random.
You can use an XPath expression to achieve this:
//tr[starts-with(@id, "link")]
Example:
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('///tr[starts-with(@id, "link")]');
foreach ($nodes as $node) {
// Do whatever
}
DOM way using some string functions ...
$dom = new DOMDocument;
$dom->loadHTML($html); $tagK = 'link_';
foreach ($dom->getElementsByTagName('tr') as $tag) {
if (substr(strtolower($tag->getAttribute('id')),0,strlen($tagK))===$tagK) {
echo $tag->getAttribute('id').PHP_EOL;
}
}
Or if you want to have more flexible way and easy to Web Scrape.. I suggest you take a look at https://github.com/fabpot/goutte which act as wrapper. that you can also used for clicking a link or submitting a form..
I made some tutorials using Goutte Class for Web Scraping.. Feel free to check it. http://iapdesign.com/webdev/laravel-4-webdev/superb-web-scraping-tutorials-using-laravel-4/
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.