How to get text in array between all <span> tag from HTML?

谁说胖子不能爱 提交于 2019-12-05 01:36:14

you need to switch to preg_match_all function

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

aleation

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

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

here is code to get all span value in array

      $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

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.

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

$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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!