當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP SimpleXMLElement __toString()用法及代碼示例

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




相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | SimpleXMLElement __toString() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。