SimpleXMLElement::__toString

(Информация о версии неизвестна, возможно, только в SVN)

SimpleXMLElement::__toStringReturns the string content

Описание

public string SimpleXMLElement::__toString ( void )

Returns text content that is directly in this element. Does not return text content that is inside this element's children.

Список параметров

У этой функции нет параметров.

Возвращаемые значения

Returns the string content on success or an empty string on failure.

Примеры

Пример #1 Get string content

<?php
$xml 
= new SimpleXMLElement('<a>1 <b>2 </b>3</a>');
echo 
$xml;
?>

Результат выполнения данного примера:

1 3

Смотрите также

  • SimpleXMLElement::asXML() - Возвращает сформированный XML документ в виде строки используя SimpleXML элемент

Коментарии

Автор:
For those for whom it may not be immediately obvious from the example, to assign the text of a node (but not its children) to a variable:

$XML = new SimpleXMLElement('<p>Hello<span> world</span>.<span> Good day!</span></p>');

Either:
$Text = $XML;
$Text = $XML->__String();

is effectively:
$Text = 'Hello.'; // The <span>s are ignored.

Either of:
$Text = $XML->span;
$Text = $XML->span[0];

is effectively:
$Text = ' world'; // Only the first <span> is used.

$Text = $XML->span[1];

is effectively:
$Text = ' Good day!'; // Only the second <span> is used.
2016-03-27 13:36:14
http://php5.kiev.ua/manual/ru/simplexmlelement.tostring.html
Автор:
[Someone remove that other Patanjali's note, because it has errors! :-(]

For those for whom it may not be immediately obvious from the example, the echo is what is forcing __toString() to be used.

However, to assign the text of a node (but not its children) to a variable:

$XML = new SimpleXMLElement('<p>Hello<span> world</span>.<span> Good day!</span></p>');

$Text = $XML->__toString();

is effectively:
$Text = 'Hello.'; // The <span>s are ignored.

Either of:
$Text = $XML->span->__toString();
$Text = $XML->span[0]->__toString();

is effectively:
$Text = ' world'; // Only the first <span> is used.

$Text = $XML->span[1]->__toString();

is effectively:
$Text = ' Good day!'; // Only the second <span> is used.
2016-03-27 13:55:17
http://php5.kiev.ua/manual/ru/simplexmlelement.tostring.html
__toString() is not intended to be called directly.

Instead, it defines what is returned when the object is cast to string, either explicitly with:

(string)$element

Or implicitly, when used in certain contexts which would cause a conversion to string.
2017-10-02 06:22:50
http://php5.kiev.ua/manual/ru/simplexmlelement.tostring.html

    Поддержать сайт на родительском проекте КГБ