0

Here is what I have so far:

My string:

$str = "<ul>
<li><a name="valuehere1" title="titlehere" href="/channel/london/">Link1</a></li>
<li><a name="valuehere2" title="titlehere" href="/channel/games/">Link1</a></li>
<li><a name="valuehere3" title="titlehere" href="/channel/sport/">Link1</a></li>
</ul>";

My PHP so far (and I am stuck):

$dom = new domDocument;
$dom->loadHTML($str);
$children = $dom->getElementsByTagName('li')->item(0)->childNodes->getAttribute('name'); 
$out = array();
foreach ($children as $child) {
    $out[] = $dom->saveXML($child);
}

I am trying to extract the NAME attribute value of the A tag in the LI base on a match (in this example they are "london", "games", "sport") . WHen I pass "games" it should give me the output as "valuehere2". This has to be done at the server side due to some restrictions I have. Can someone help me with this please?

Thanks, L

lshettyl
  • 8,166
  • 4
  • 25
  • 31
  • Your string has double quotes in it - escape them by making them: \" – Tanner Ottinger Mar 15 '11 at 16:33
  • why are you fetching the li first, when you want the a elements? Why dont you fetch the a elements directly? Or use XPath to fetch the name attributes of those a elements directly? – Gordon Mar 15 '11 at 18:22
  • possible duplicate of [(PHP5) Extracting a title tag and RSS feed address from HTML using PHP DOM or Regex](http://stackoverflow.com/questions/3054347/php5-extracting-a-title-tag-and-rss-feed-address-from-html-using-php-dom-or-reg) - covers the same grounds and shows how to do it with XPath. – Gordon Mar 15 '11 at 18:24
  • and also possible duplicate of [php regular expression to match specific url pattern](http://stackoverflow.com/questions/2532358/php-regular-expression-to-match-specific-url-pattern) – Gordon Mar 15 '11 at 18:30

2 Answers2

1

You've almost got it. But your code is fetching an attribute of the first li it finds, and tries to use that attribute value as an array to loop on. What you want is:

$children = $dom->getElementsByTagName('li');
$out = array();
foreach ($children as $child) {
    if ($child->item(0)->childNodes->getAttribute('name')) {
        $out[] = $dom->saveXML($child);
    }
}

getElementsByTagName returns an DOMElementList (or whatever), which is an iterable array. Doing the getAttribute() stuff simply returns a string.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • `getElementByTagName` returns a [`DOMNodeList`](http://de2.php.net/manual/en/class.domnodelist.php), which is not an array but an object. [`childNodes`](http://de2.php.net/manual/en/class.domnode.php#domnode.props.childnodes) is a `DOMNodeList`, too and it has no `getAttribute` method. The code cannot work. – Gordon Mar 15 '11 at 18:13
0

Regular expressions to the rescue?

[~]%  cat test.php
<?php

$str = '<ul>
<li><a name="valuehere1" title="titlehere" href="/channel/london/">Link1</a></li>
<li><a name="valuehere2" title="titlehere" href="/channel/games/">Link1</a></li>
<li><a name="valuehere3" title="titlehere" href="/channel/sport/">Link1</a></li>
</ul>';

preg_match_all('/<li><a name="(.*)" title/', $str, $m);

print_r($m);

?>

[~]% php test.php
Array
(
    [0] => Array
        (
            [0] => <li><a name="valuehere1" title
            [1] => <li><a name="valuehere2" title
            [2] => <li><a name="valuehere3" title
        )

    [1] => Array
        (
            [0] => valuehere1
            [1] => valuehere2
            [2] => valuehere3
        )

)
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Gah. parsing HTML with regexes. horrible horrible horrible. – Marc B Mar 15 '11 at 16:29
  • It depends on the context and what exactly you're parsing ... The question is posted without context, so I don't know which solution is best in this case (I assume that the $str posted is just a snippet from a larger page?), but I certainly wouldn't say that extracting data from HTML using the DOM is "horrible horrible horrible" ;-) IMHO both methods have advantages and disadvantages. – Martin Tournoij Mar 15 '11 at 16:47
  • Thanks CS, I have built on your suggestion and it fits well into my space. thanks :) – lshettyl Mar 15 '11 at 18:07