简体   繁体   English

如何获取标签内容?

[英]How to get tag content?

Im making a script to get other pages content, and right now im working on a function that should get tag content... but im a bit stuck :D 我正在制作一个脚本来获取其他页面内容,现在我正在开发一个应该获得标签内容的功能......但我有点卡住:D

found a new tag of same kind inside tag...
nothing found...
1111
2222

is printed. 打印出来。

<?php

function d($toprint)
{
    echo $toprint."<br />";
}

function GetTagContents($source, $tag, $pos)
{   
    $startTagPos        = strpos( $source, "<".$tag, $pos );
    $startTagEndPos     = strpos( $source, ">", $startTagPos )+1;

    $endTagPos          = strpos( $source, "</".$tag, $startTagEndPos);

    $lastpos = $startTagPos+1;    
    while( $lastpos != False )
    {
        $newStartTagPos = strpos( $source, "<".$tag, $lastpos );

        if( $newStartTagPos == False )
        {
            d("nothing found...");
            $lastpos = False;        
        }
        else if( $newStartTagPos > $endTagPos )
        {
            d("out of bounds...");
            $lastpos = False;
        }
        else
        {
            d("found a new tag of same kind inside tag...");
            $lastpos =  $newStartTagPos+1;       
            $endTagPos  = strpos( $source, "</".$tag, $newStartTagPos);
        }
    }

    return substr($source, $startTagEndPos, $endTagPos-$startTagEndPos);
}
?>
<html>

    <body>
    <?php

    d(GetTagContents('<div>1111<div>2222</div>3333</div>', "div", 0));

    ?>
    </body>

</html>

someone got any ideas? 有人有任何想法吗?

Using PHP DOM: 使用PHP DOM:

$src = new DOMDocument('1.0', 'utf-8');
$src->formatOutput = true;
$src->preserveWhiteSpace = false;
$src->load('path/to/file.html');

$tagName = 'foo';
$element = $src->getElementsByTagName($tagName)->item(0);
var_dump($element->nodValue)

strpos will return 0 the first time, and 0 == false in PHP. strpos第一次返回0,在PHP中返回0 0 == false The check you want is to compare the result with === , which evaluates to true if both values are the same value and the same type. 您要进行的检查是将结果与===进行比较,如果两个值都是相同的值和相同的类型,则结果为true。 That is, 0 == false is true but 0 === false is not true. 也就是说, 0 == false为真,但0 === false不为真。

you can use this 你可以用它

simplexml_load_string

$xml = "[div]1111[div]2222[/div]3333[/div]";

$loadStrring = simplexml_load_string($xml);
foreach($loadStrring->children() as $name => $data) {
    if($name ='div')
        echo $data . "\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM