0

In this code

<root>
    <main>
        <cont>
            <p>hello<a>world</a></p>
            <p>hello</p>
            <p>hello<a>world</a></p>
        </cont>
    </main>
</root>

I just need to get only the text inside <cont> tag. without getting <a> tag and its contents

so, the results will be hello hello hello without world

2 Answers 2

1

You can select the text nodes that are a direct descendant of each <p> tag:

$dom = new DOMDocument;
$dom->loadXml($xmlData);

$xpath = new DOMXpath($dom);

foreach ($xpath->query('//cont/p/text()') as $text) {
    echo $text->textContent, "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

1

A simplexml_load_string() or simplexml_load_file() should be enough:

$xml_string = '<root> <main> <cont> <p>hello<a>world</a></p> <p>hello</p> <p>hello<a>world</a></p> </cont> </main></root>';
$xml = simplexml_load_string($xml_string);
$p = $xml->main->cont->p;
foreach($p as $value) {
    $parapgraphs[] = (string) $value;
}

echo '<pre>';
print_r($parapgraphs);

Should show something like:

Array
(
    [0] => hello
    [1] => hello
    [2] => hello
)

1 Comment

The problem is that <a> is inside <p>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.