1

i managed to load a table (4x4 fields, links in first row) to simplexml, where on a link I expect returning only inner html of the link (bolds etc) but it returns whole element

  foreach($xml->tbody->children() as $tr){
  $row++;
  for ($i=0;$i<4;$i++)   {  
     $data[$row][$i]= $tr->td[$i]->asXML();   
     if($row==1)
     { $href[$i] = (string)$tr->td[$i]->a[0]['href'];
       $titl[$i] = $tr->td[$i]->a[0]->asXML(); // PROBLEMATIC POINT
     }
  }  

expected: 'link<b>text</b>'
returned: '<a href="....">link<b>text</b></a>'  /e.g. whole 'a' element/

if I would add the (string) type setting, I would loose the inner formating of the link,
while the above $tr->td[$i] returns only inner content of element td

hakre
  • 193,403
  • 52
  • 435
  • 836
Peminator
  • 783
  • 1
  • 9
  • 24
  • just noticed trying to use (string) typesetting returns only unwrapped a text so any bold would be omitted ... bypassable by strip_tags to getplain text of link – Peminator Feb 26 '13 at 23:13
  • SimpleXML has nothing to return inner XML of an element out of the box. So what is your question? – hakre Feb 26 '13 at 23:26
  • then why if I do `$tr->td[$i]` it returns for example `hello` /inner content of that element/ instead of `hello` ?? – Peminator Feb 26 '13 at 23:32
  • i'd like to achieve using only built-in php classes so no external classes solutions, if possible – Peminator Feb 26 '13 at 23:44
  • 1
    I think this is one of those occasions where I'd go with the DOM over SimpleXML, as in this answer: http://stackoverflow.com/a/7128991/157957 The reason being you need to iterate over both the text nodes and child elements, which SimpleXML simplifies out of your reach... – IMSoP Feb 27 '13 at 01:34
  • @Peminator: That is because it is the string (`__toString()`) value of that ``-element representing SimpleXMLElement. It is plain text - not XML. Try `$tr->td[$i]->asXML()` instead and you'll see that it is with the surrounding `...` tags. – hakre Feb 28 '13 at 09:18
  • An IMSoP is right in my eyes, this is why I asked for more infos. I would have linked the exact same duplicate then. – hakre Feb 28 '13 at 09:19

1 Answers1

1

Hope it helps:

$titl[$i] = $tr->td[$i]->a[0]->children()->asXML();
apoq
  • 1,454
  • 13
  • 14
  • ooooh thought u saved my day, but after trying it returns,based on the example above only the `text` instead `linktext` - the non-bold text is missing... – Peminator Feb 26 '13 at 23:40
  • oops, just noticed that – apoq Feb 26 '13 at 23:42
  • On SimpleXMLElement objects representing zero or more elements (like the return value of `children()`), the method normally only works for the first element (like the method named `asXML()`). SimpleXML has only limited support (if one would say so at all) for textnodes. – hakre Feb 28 '13 at 09:13