如何从HTML中的所有标记之间获取数组中的文本?

时间:2022-11-13 20:22:22

I want to fetch text in array between all <span> </span> tag from HTML, I have tried with this code but it returns only one occurrence :

我想从HTML中获取所有 标记之间的数组中的文本,我已尝试使用此代码,但它只返回一次:

preg_match('/<span>(.+?)<\/span>/is', $row['tbl_highlighted_icon_content'], $matches);

echo $matches[1]; 

My HTML:

我的HTML:

<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce.  Who can combine the wholly incompatible, and make a unity  of what can NEVER j<span>oin? Walk </span>you the gentle way,

My code returns only one occurrence of span tag, but I want get all text from every span tag in HTML in the form of a php array.

我的代码只返回一次span标记,但我希望以php数组的形式从HTML中的每个span标记中获取所有文本。

4 个解决方案

#1


2  

you need to switch to preg_match_all function

你需要切换到preg_match_all函数

Code

$row['tbl_highlighted_icon_content'] = '<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way,';    

preg_match_all('/<span>.*?<\/span>/is', $row['tbl_highlighted_icon_content'], $matches);

var_dump($matches);

as you can see now array is correctly populated so you can echo all your matches

正如您所看到的,现在阵列已正确填充,因此您可以回显所有匹配项

#2


2  

use preg_match_all() it's the same, it will return all the occurrences in the $matches array

使用preg_match_all()它是相同的,它将返回$ matches数组中的所有匹配项

http://php.net/manual/en/function.preg-match-all.php

http://php.net/manual/en/function.preg-match-all.php

#3


1  

here is code to get all span value in array

这是获取数组中所有span值的代码

      $str = "<span>The wish to</span> be unfairly treated is a compromise
attempt that would COMBINE attack <span>and innocen</span>ce. 
Who can combine the wholly incompatible, and make a unity 
of what can NEVER j<span>oin? Walk </span>you the gentle way,";

preg_match_all("/<span>(.+?)<\/span>/is", $str, $matches);


echo "<pre>";
print_r($matches);

you output will be

你的输出会是

Array
(
    [0] => Array
        (
            [0] => The wish to
            [1] => and innocen
            [2] => oin? Walk 
        )

    [1] => Array
        (
            [0] => The wish to
            [1] => and innocen
            [2] => oin? Walk 
        )

)

you can use o or 1 index

你可以使用o或1索引

#4


0  

If you don't mind using a third-party component, I'd like to show you Symfony's DomCrawler component. It 's a very simple way to parse HTML/XHTML/XML files and navigate through the nodes.

如果您不介意使用第三方组件,我想向您展示Symfony的DomCrawler组件。这是解析HTML / XHTML / XML文件并在节点中导航的一种非常简单的方法。

You can even use CSS Selectors. Your code would be something like:

您甚至可以使用CSS选择器。你的代码是这样的:

$crawler = new Crawler($html);
$spans = $crawler->filter("span");
echo $spans[1]->getText();;

You don't even need to have a full HTML/XML document, if you assign only the <span>...</span> part of your code, it'll work fine.

您甚至不需要拥有完整的HTML / XML文档,如果只分配代码的 ... 部分,它将正常工作。

#1


2  

you need to switch to preg_match_all function

你需要切换到preg_match_all函数

Code

$row['tbl_highlighted_icon_content'] = '<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way,';    

preg_match_all('/<span>.*?<\/span>/is', $row['tbl_highlighted_icon_content'], $matches);

var_dump($matches);

as you can see now array is correctly populated so you can echo all your matches

正如您所看到的,现在阵列已正确填充,因此您可以回显所有匹配项

#2


2  

use preg_match_all() it's the same, it will return all the occurrences in the $matches array

使用preg_match_all()它是相同的,它将返回$ matches数组中的所有匹配项

http://php.net/manual/en/function.preg-match-all.php

http://php.net/manual/en/function.preg-match-all.php

#3


1  

here is code to get all span value in array

这是获取数组中所有span值的代码

      $str = "<span>The wish to</span> be unfairly treated is a compromise
attempt that would COMBINE attack <span>and innocen</span>ce. 
Who can combine the wholly incompatible, and make a unity 
of what can NEVER j<span>oin? Walk </span>you the gentle way,";

preg_match_all("/<span>(.+?)<\/span>/is", $str, $matches);


echo "<pre>";
print_r($matches);

you output will be

你的输出会是

Array
(
    [0] => Array
        (
            [0] => The wish to
            [1] => and innocen
            [2] => oin? Walk 
        )

    [1] => Array
        (
            [0] => The wish to
            [1] => and innocen
            [2] => oin? Walk 
        )

)

you can use o or 1 index

你可以使用o或1索引

#4


0  

If you don't mind using a third-party component, I'd like to show you Symfony's DomCrawler component. It 's a very simple way to parse HTML/XHTML/XML files and navigate through the nodes.

如果您不介意使用第三方组件,我想向您展示Symfony的DomCrawler组件。这是解析HTML / XHTML / XML文件并在节点中导航的一种非常简单的方法。

You can even use CSS Selectors. Your code would be something like:

您甚至可以使用CSS选择器。你的代码是这样的:

$crawler = new Crawler($html);
$spans = $crawler->filter("span");
echo $spans[1]->getText();;

You don't even need to have a full HTML/XML document, if you assign only the <span>...</span> part of your code, it'll work fine.

您甚至不需要拥有完整的HTML / XML文档,如果只分配代码的 ... 部分,它将正常工作。