【问题标题】:Find and extract repeated occurrences of a string in text including tags查找并提取文本中重复出现的字符串,包括标签
【发布时间】:2014-01-07 08:40:27
【问题描述】:

假设我有这段文字:

The <b>quick brown</b> fox jumps over the lazy dog
The quick brown fox jumps over the <b>lazy dog</b>
The quick brown fox <b>jumps over</b> the lazy dog

我想从上面的文本中获取并提取所有出现的这个字符串:

<b>quick brown</b>
<b>lazy dog</b>
<b>jumps over</b>

现在我知道我需要一个 while 循环来检查直到文本的结尾和一些字符串函数,但我不确定是哪些。

如果有人可以帮助解决这个问题。

【问题讨论】:

  • 你可以使用正则表达式或者一些子字符串(substr php 命令)。
  • @user481913,终于更新了我的回答,完全符合你的问题!
  • 编辑 - 和 之间的文本并不总是相同...更改了上面的问题以反映这一点。我正在尝试提供的各种解决方案,看看哪一个最适合...
  • @IliaRostovtsev - 谢谢,我会试试...
  • @user481913 不客气!如果您需要更多帮助,请告诉我!

标签: php string substring


【解决方案1】:

这样做..

<?php
$html='The <b>quick brown</b> fox jumps over the lazy dog
The <b>quick brown</b> fox jumps over the lazy dog
The <b>quick brown</b> fox jumps over the lazy dog';

function funcx($v)
{
    return "<b>".$v."</b>";
}

preg_match_all('~<b>(.*?)<\/b>~', $html, $matches);
$results=array_map('funcx',$matches[1]);
var_dump($results);

输出:

array (size=3)
  0 => string '<b>quick brown</b>' (length=18)
  1 => string '<b>quick brown</b>' (length=18)
  2 => string '<b>quick brown</b>' (length=18)

【讨论】:

    【解决方案2】:
    $text = "The <b>quick brown</b> fox jumps over the lazy dog
    The <b>quick brown</b> fox jumps over the lazy dog
    The <b>quick brown</b> fox jumps over the lazy dog";
    $part = "<b>quick brown</b>";
    $count = substr_count($text, $part);
    for($i=0;$i<$count;$i++)
    {
    echo $part."<br>";
    }
    

    输出

    快速棕色

    快速棕色

    快速棕色

    如果你替换

    echo $part."&lt;br&gt;";

    与

    echo htmlspecialchars($part)."&lt;br&gt;";

    输出

    <b>quick brown</b>
    <b>quick brown</b>
    <b>quick brown</b>
    

    【讨论】:

    • 这与我已经发布的答案相同,您只需要在解决方案中添加更多代码。
    【解决方案3】:

    如果您想使用正则表达式,请尝试以下操作:

    /<b ?.*>(.*)<\/b>/
    

    它将捕获 &lt;b&gt;&lt;/b&gt; 标签内的所有内容,包括标签本身。

    Online Example

    您可以使用简单的函数将上述正则表达式的用法扩展到不止一个 &lt;b&gt; 标记,并传递一个您想要捕获的标记:

    例子:

    function getTextBetweenTags($string, $tagname)
    {
        $pattern = '/<'.$tagname.'>.*?<\/'.$tagname.'>/is';
        preg_match_all($pattern, $string, $matches);
        return $matches;
    }
    

    用法:

    $string = 'The <b>quick brown</b> fox jumps over the lazy dog \
               The <b>quick black</b> fox jumps over the lazy dog \
               The <b>quick white</b> fox jumps over the lazy dog';
    $text = getTextBetweenTags($string, "b");
    print_r($text);
    

    输出:

    Array
    (
        [0] => Array
            (
                [0] => <b>quick brown</b>
                [1] => <b>quick black</b>
                [2] => <b>quick white</b>
            )
    
    )
    

    Online Example

    编辑 1:

    我已经为你扩展了上面的功能,所以它可以使用多个标签:

    例子:

    function getTextBetweenTags($string, $tagsname)
    {
        $tagsname = explode(',',$tagsname);
        foreach ($tagsname as $tagname) 
        {
            $pattern = '/<'.$tagname.'>.*?<\/'.$tagname.'>/is';
            preg_match_all($pattern, $string, $matches);
            $results[] = $matches;
        }
        return $results;
    }
    

    用法:

    $string = 'The <b>quick brown</b> fox jumps <strong>over</strong> the lazy dog \
               The <b>quick black</b> fox jumps over the <span>lazy</span> dog \
               The <b>quick white</b> fox jumps over the lazy dog';
    $text = getTextBetweenTags($string, "b,strong,span"); // Single or multiple HTML tags
    print_r($text);
    

    输出:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [0] => <b>quick brown</b>
                        [1] => <b>quick black</b>
                        [2] => <b>quick white</b>
                    )
    
            )
    
        [1] => Array
            (
                [0] => Array
                    (
                        [0] => <strong>over</strong>
                    )
    
            )
    
        [2] => Array
            (
                [0] => Array
                    (
                        [0] => <span>lazy</span>
                    )
    
            )
    
    )
    

    Online Example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-13
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2019-12-24
      • 1970-01-01
      • 2021-01-29
      相关资源
      最近更新 更多