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


PHP DOMNode getLineNo()用法及代碼示例


DOMNode::getLineNo()函數是PHP中的內置函數,用於獲取定義節點的行號。

用法:

DOMNode DOMNode::getLineNo( void )

參數:此函數不接受任何參數。


返回值:此函數返回定義節點所在的行號。

下麵給出的程序說明了PHP中的DOMNode::getLineNo()函數:程序1:

<?php 
// Create a XML variable 
$xml = <<<XML 
<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <h1>GeeksforGeeks</h1> 
</root> 
XML; 
  
// Create a new DOMDocument instance 
$dom = new DOMDocument; 
  
// Load the XML 
$dom->loadXML($xml); 
  
// Print where the line where the 'node' element was defined in 
echo 'The <node> tag is defined on line ' . $dom->getElementsByTagName('h1')->item(0)->getLineNo(); 
?>

輸出:

The tag is defined on line 3

程序2:

<?php 
// Create a XML variable 
$xml = <<<XML 
<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <h1>Geeks</h1> 
    <h1>For</h1> 
    <h1>Geeks</h1> 
</root> 
XML; 
  
// Create a new DOMDocument instance 
$dom = new DOMDocument(); 
  
// Load the XML 
$dom->loadXML($xml); 
  
for ($i = 0; $i < 3; $i++) { 
    // Print where the line where the 'node' element was defined in 
    echo $i . ') The h1 tag is defined on line ' . $dom->getElementsByTagName('h1')->item($i)->getLineNo() . "<br>"; 
  
} 
?>

輸出:

0) The h1 tag is defined on line 3
1) The h1 tag is defined on line 4
2) The h1 tag is defined on line 5

參考: https://www.php.net/manual/en/domnode.getlineno.php



相關用法


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