2

我想提取以下文本之间的任何文本或字符串<p><b><div id="t" class="t">这是我的示例,它不起作用

$st = '<p><b>Auburn</b> is a city in <a href="/my/id/ala" title="auburn">Lee County</a>, <a href="/my/Alabama" title="Alabama">Alabama</a>, <a href="/my/ph" title="PH">United States</a>. It is the largest city in eastern Alabama with a 2012 population of 56,908.<sup id="test" class="test"><a href="#tst"><span>[</span>2<span>]</span></a></sup> It is a principal city of the <a href="/my/tst" title="Auburn-Opelika Metropolitan Area" class="cs">Auburn-Opelika Metropolitan Area</a>. The <a href="/my/st" title="Auburn-Opelika, AL MSA" class="vf">Auburn-Opelika, AL MSA</a> with a population of 140,247, along with the <a href="/myu/sc" title="Columbus, GA-AL MSA" class="Xd">Columbus, GA-AL MSA</a> and <a href="/my/fd" title="Tuskegee, Alabama">Tuskegee, Alabama</a>, comprises the greater <a href="/my/cdA" title="Columbus-Auburn-Opelika, GA-AL CSA" class="se">Columbus-Auburn-Opelika, GA-AL CSA</a>, a region home to 456,564 residents.</p>
<p>Auburn is a <a href="/my/te" title="College town">college town</a> and is the home of <a href="/my/As" title="Auburn University">Auburn University</a>. Auburn has been marked in recent years by rapid growth, and is currently the fastest growing metropolitan area in Alabama and the nineteenth-fastest growing metro area in the United States since 1990.<sup class="fd" style="white-space:nowrap;">[<i><a href="/my/d" title="fda"><span title="fad (August 2011)">citation needed</span></a></i>]</sup> U.S. News ranked Auburn among its top ten list of best places to live in United States for the year 2009.<sup id="d3" class="f"><a href="3"><span>[</span>3<span>]</span></a></sup> The city`s unofficial nickname is “The Loveliest Village On The Plains,” taken from a line in the poem <i><a href="/my/da" title="The Deserted Village">The Deserted Village</a></i> by <a href="/my/fs" title="Oliver Goldsmith">Oliver Goldsmith</a>: “Sweet Auburn! loveliest village of the plain...”&lt;sup id="ds" class="dsa"><a href="dd"><span>[</span>4<span>]</span></a></sup></p>
<div id="t" class="t">';

preg_match_all('/<p><b>(.*?)<div id="t" class="t">/U', $st, $output);
$result = $output[0];
print_r($output);
echo $result;
4

2 回答 2

1

这里不需要正则表达式,因为我们正在使用文字字符串。只需使用strpos偏移量:

<?php
    function str_between($string, $searchStart, $searchEnd, $offset = 0) {
        $startPosition = strpos($string, $searchStart, $offset);
        if ($startPosition !== false) {
            $searchStartLength = strlen($searchStart);
            $endPosition = strpos($string, $searchEnd, $startPosition + 1);
            if ($endPosition !== false) {
                return substr($string, $startPosition + $searchStartLength, $endPosition - $searchStartLength);
            }
            return substr($string, $startPosition + $searchStartLength);
        }
        return $string;
    }

    var_dump(str_between($st, '<p><b>', '<div id="t" class="t">'));
?>

演示

于 2013-10-24T16:06:04.403 回答
0

如果您仍想使用它而不是 h2ooooooo 的答案,则稍作修改将有助于您的正则表达式:

“/s”告诉正则表达式继续搜索超出换行符。您的 $st 包含正则表达式引擎停止的换行符。

使用以下:

preg_match_all('/<p><b>(.*?)<div id="t" class="t">/sU', $st, $output);
于 2013-10-24T18:10:09.437 回答