XMLReader::__toString()函數是PHP中的內置函數,用於獲取直接位於當前元素中的文本內容。此函數不返回該元素的子元素內的文本內容。
用法:
void XMLReader::__toString( void )
參數:此函數不接受任何參數。
返回值:如果成功,則此函數返回字符串內容;如果失敗,則返回空字符串。
以下示例說明了PHP中的XMLReader::__toString()函數:
範例1:
<?php
// Create the XML
$xmlstr = <<<XML
<?xml version='1.0'?>
<library>
GeeksforGeeks
</library>
XML;
// Create a new SimpleXMLElement
$SXE = new SimpleXMLElement($xmlstr);
// Get the content
$string = $SXE->__toString();
echo $string;
?>
輸出:
GeeksforGeeks
範例2:
<?php
// Create the XML
$xmlstr = <<<XML
<?xml version='1.0'?>
<div>
<sub>
This text should not be visible
</sub>
</div>
XML;
// Create a new SimpleXMLElement
$SXE = new SimpleXMLElement($xmlstr);
// Add the attribute
$string = $SXE->__toString();
echo $string;
?>
輸出:
// Empty string because text content is in a childern node not current node.
參考: https://www.php.net/manual/en/simplexmlelement.tostring.php
相關用法
- PHP SimpleXMLElement addChild()用法及代碼示例
- PHP SimpleXMLElement getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement getNamespaces()用法及代碼示例
- PHP SimpleXMLElement addAttribute()用法及代碼示例
- PHP SimpleXMLElement saveXML()用法及代碼示例
- PHP SimpleXMLElement XPath()用法及代碼示例
- PHP SimpleXMLElement asXML()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- PHP SimpleXMLElement count()用法及代碼示例
- PHP SimpleXMLElement::getName()用法及代碼示例
- PHP SimpleXMLElement attributes()用法及代碼示例
- PHP SimpleXMLElement registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement::__construct()用法及代碼示例
- d3.js color.toString()用法及代碼示例
- Javascript symbol.toString()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | SimpleXMLElement __toString() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。