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

4

11

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 :

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

echo $matches[1]; 

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

Counseloratlaw answered 15/4, 2013 at 12:53 Comment(2)
Parsing HTML with regex?Celisse
I have a similar question here stackoverflow.com/q/34569269/5477982Firstly
C
2

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

Carnauba answered 15/4, 2013 at 13:0 Comment(0)
F
2

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

Fleta answered 15/4, 2013 at 12:56 Comment(2)
One cannot parse HTML with a regex. ever.Versed
like everything, it depends on the context, it's up to the programmer to know what does his data looks like, if it's just span tags with no special content inside, I don't see why notFleta
C
2

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

Carnauba answered 15/4, 2013 at 13:0 Comment(0)
E
1

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

Exorable answered 15/4, 2013 at 13:1 Comment(0)
G
1

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.

Gabar answered 15/4, 2013 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.