(PHP 5, PHP 7, PHP 8)
tidyNode::isText — 檢查節點是否代表文字(無標記)
此函式沒有參數。
範例 #1 從混合的 HTML 文件中擷取文字
<?php
$html = <<< HTML
<html><head>
<?php echo '<title>title</title>'; ?>
<#
/* JSTE 程式碼 */
alert('Hello World');
#>
</head>
<body>
<?php
// PHP 程式碼
echo 'hello world!';
?>
<%
/* ASP 程式碼 */
response.write("Hello World!")
%>
<!-- 註解 -->
Hello World
</body></html>
HTML 外部
HTML;
$tidy = tidy_parse_string($html);
$num = 0;
get_nodes($tidy->html());
function get_nodes($node) {
// 檢查目前的節點是否為所需的類型
if($node->isText()) {
echo "\n\n# 文字節點 #" . ++$GLOBALS['num'] . "\n";
echo $node->value;
}
// 檢查目前的節點是否有子節點
if($node->hasChildren()) {
foreach($node->child as $child) {
get_nodes($child);
}
}
}
?>
以上範例將輸出
# text node #1 Hello World # text node #2 Outside HTML