Как сохранить Domdocument без HTML -обертки?Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как сохранить Domdocument без HTML -обертки?

Сообщение Anonymous »

Я функция ниже, я изо всех сил пытаюсь вывести Domdocument без добавления обертков XML, HTML, Body и p до выхода содержание. Предлагаемое исправление: < /p>
$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));
< /code>

работает только тогда, когда в нем нет элементов уровня блока. Однако, когда это происходит, как в примере ниже с элементом H1, полученный выход из SaveXML усекается до ... < /p>


< P> Если вам нравится < /p> < /p>
< /blockquote>

Я был указан на этот пост как возможный обходной путь, но я не могу Поймите, как внедрить его в это решение (см. Прокомментированные попытки ниже). < /p>

Любые предложения? < /p>

function rseo_decorate_keyword($postarray) {
global $post;
$keyword = "Jasmine Tea"
$content = "If you like jasmine tea you will really like it with Jasmine Tea flavors. This is the last ocurrence of the phrase jasmine tea within the content. If there are other instances of the keyword jasmine tea within the text what happens to jasmine tea."
$d = new DOMDocument();
@$d->loadHTML($content);
$x = new DOMXpath($d);
$count = $x->evaluate("count(//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword') and (ancestor::b or ancestor::strong)])");
if ($count > 0) return $postarray;
$nodes = $x->query("//text()[contains(translate(., 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$keyword') and not(ancestor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6) and not(ancestor::b) and not(ancestor::strong)]");
if ($nodes && $nodes->length) {
$node = $nodes->item(0);
// Split just before the keyword
$keynode = $node->splitText(strpos($node->textContent, $keyword));
// Split after the keyword
$node->nextSibling->splitText(strlen($keyword));
// Replace keyword with keyword

$replacement = $d->createElement('strong', $keynode->textContent);
$keynode->parentNode->replaceChild($replacement, $keynode);
}
$postarray['post_content'] = $d->saveXML($d->getElementsByTagName('p')->item(0));
// $postarray['post_content'] = $d->saveXML($d->getElementsByTagName('body')->item(1));
// $postarray['post_content'] = $d->saveXML($d->getElementsByTagName('body')->childNodes);
return $postarray;
}


Подробнее здесь: https://stackoverflow.com/questions/487 ... ml-wrapper
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»