Getting the text portion of a node using php Simple XML

前端 未结 7 2009
臣服心动
臣服心动 2020-12-03 18:39

Given the php code:

$xml = <<
This is a link Title with some text following it.
相关标签:
7条回答
  • 2020-12-03 19:42

    You can get the text node of a DOM element with simplexml just by treating it like a string:

    foreach($xml->children() as $x) {
       $result .= "$x"
    

    However, this prints out:

    This is a link
    
    with some text following it.
    TitleTitle
    

    ..because the text node is treated as one block and there is no way to tell where the child fits in inside the text node. The child node is also added twice because of the other else {}, but you can just take that out.

    Sorry if I didn't help much, but I don't think there's any way to find out where the child node fits in the text node unless the xml is consistent (but then, why not use tags). If you know what element you want to strip the text out of, strip_tags() will work great.

    0 讨论(0)
提交回复
热议问题