Javascript PHP在HTML节点之间获取文本

Javascript PHP在HTML节点之间获取文本,javascript,php,html,arrays,regex,Javascript,Php,Html,Arrays,Regex,标题说明一切。如何使用PHP获取HTML节点之间的文本?有什么想法吗? 下面是我的HTML结构 <html> <head> <title>Test Page</title> </head> <body> <div id="outer"> <div id="first"> <p class="this">Hello</p>

标题说明一切。如何使用PHP获取HTML节点之间的文本?有什么想法吗? 下面是我的HTML结构

<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="outer">
        <div id="first">
            <p class="this">Hello</p>
            <p class="this">Community</p>
        </div>
        <div id="second">
            <p class="that">Stack</p>
            <p class="that">Overflow</p>
        </div>
    </div>
</body>
试试这个

function getTextBetweenTags($string, $tagname)
 {
    $pattern = "/<$tagname>(.*?)<\/$tagname>/";
    preg_match($pattern, $string, $matches);
    return $matches[1];
 }
函数getTextBetweenTags($string,$tagname) { $pattern=“/(**?/”; 预匹配($pattern,$string,$matches); 返回$matches[1]; } 您必须循环使用$matches数组…

您可以尝试:

$text = strip_tags($html);

那会让你走得很远。它会留下空间并返回,但这些很容易删除

$clean = str_replace(array(' ',"\n","\r"),'',$text);

在您的示例中使用它可以给出:

TestPageHelloCommunityStackOverflow
如果您想保留一些完整的空间,可以尝试:

$clean = trim(implode('',explode("\n",$text)));
其结果是:

Test Page Hello Community Stack Overflow

很多变体都是可能的。

强烈建议不要使用正则表达式来解析HTML。
使用简单HTML库:
包括它:
Include'simple_html_dom.php'
获取所需的标记:
$tags=$html->find('p')
创建数组:
$a=array();foreach($tags as$tag)$a[]=$tag->innertext

创建字符串:
$string=$a[0]$a[2]$a[3]$a[1]

我建议您使用内置PHP,而不是像simplehtmldom这样的第三方类

在大的HTML文件上,它们的速度非常慢(我曾经使用过它们)


这很简单,在这里获取PHP简单HTML DOM解析器:

然后使用以下代码:

/* include simpledom*/
include('simple_html_dom.php');

/* load html string */
$html_string = <<<HTML
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="outer">
        <div id="first">
            <p class="this">Hello</p>
            <p class="this">Community</p>
        </div>
        <div id="second">
            <p class="that">Stack</p>
            <p class="that">Overflow</p>
        </div>
    </div>
</body>
</html>
HTML;

/* create simple dom object from html */
$html = str_get_html($html_string);

/* find all paragraph elements */
$paragraph = $html->find('div[id=outer] div p');

/* loop through all elements and get inner text */
foreach($paragraph as $p){
    echo $p->innertext;
}
/*包括simpledom*/
包括('simple_html_dom.php');
/*加载html字符串*/

$html\u string=使用html解析器库,如
DOMDocument
PHP简单html DOM
。使用regexp解析html不是个好主意。您有没有尝试过什么?如果是的话,你可以把代码贴出来。你可能还想看看脱衣舞标签()玩这个:@jurgemaister该死的,打败我吧!
<?php
$html ='
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="outer">
        <div id="first">
            <p class="this">Hello</p>
            <p class="this">Community</p>
        </div>
        <div id="second">
            <p class="that">Stack</p>
            <p class="that">Overflow</p>
        </div>
    </div>
</body>
';

// a new dom object
$dom = new domDocument; 
$dom->preserveWhiteSpace = false;

// load the html into the object
$dom->loadHTML($html); 
// get the body tag
$body = $dom->getElementsByTagName('body')->item(0);
 // loop through all tags
foreach($body->getElementsByTagName('*') as $element ){
    // print the textValue
    print $element->firstChild->textContent;
}
/* include simpledom*/
include('simple_html_dom.php');

/* load html string */
$html_string = <<<HTML
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="outer">
        <div id="first">
            <p class="this">Hello</p>
            <p class="this">Community</p>
        </div>
        <div id="second">
            <p class="that">Stack</p>
            <p class="that">Overflow</p>
        </div>
    </div>
</body>
</html>
HTML;

/* create simple dom object from html */
$html = str_get_html($html_string);

/* find all paragraph elements */
$paragraph = $html->find('div[id=outer] div p');

/* loop through all elements and get inner text */
foreach($paragraph as $p){
    echo $p->innertext;
}