M
M
Mike2016-08-07 16:40:49
PHP
Mike, 2016-08-07 16:40:49

How to get to the middle of the text using xpath in php?

Friends, tell me how to get to the middle of the text, decorated with paragraphs, using xpath? The implode-explode method is not suitable, since some paragraphs are in the blockquote tag, they do not need to be considered. It turns out that as an argument in //p[i] xpath, instead of i, substitute the sum of all elements p (paragraph), excluding those that are in the blockquote tag and divide it in half, but there can still be a non-integer argument, it also needs to be rounded .. .

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya, 2016-08-08
@dillix

So you did almost everything yourself. In order not to take those paragraphs that are inside the blockquote, you need to transform your xpath a little:

//p[position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5)][not(parent::blockquote)]

I
Ivan Koryukov, 2016-08-07
@MadridianFox

XPath is a way to get to an element, but not to edit the DOM.

M
Mike, 2016-08-07
@dillix

Ivan Koryukov , well, in php, you can add an element to the DOM with it, here's a small example:

$xml = new DomDocument();
$xml->preserveWhitespace = false;
$xml->load('contacts.xml');
$xpath = new DOMXPath($xml);
$eva = $xpath->query('/contacts/person[.="Eva"]')->item(0);
$john = $xml->createElement('person', 'John');
$eva->parentNode->insertBefore($john, $eva->nextSibling);

I now need to get through xpath to the middle paragraph, I'll master the rest myself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question