Artur Stary
Artur Stary

Reputation: 744

PHP preg_match_all - how to get a content from HTML?

$Content contains HTML document

   $contents = curl_exec ($ch)  

I need to get a content from:

 <span class="Menu1">Artur €2000</span>

It's repeated several times so I want to save it into Array

I try to do that this way:

 preg_match_all('<span class=\"Menu1\">(.*?)</span>@si',$contents,$wynik2);

But I've got an error

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '('

Can You guys help me please? EDIT: $contents = curl_exec ($ch)

SOLVED: The error was cased becasue of wrong HTML on CURLed website:

  <span class="Menu1">Content</tr>

instead of:

 <span class="Menu1">Content</tr>

I didn't expected that someone can write wrong HTML. Thank You guys for help!

Upvotes: 1

Views: 3333

Answers (2)

flowfree
flowfree

Reputation: 16462

You forgot the first delimiter (@):

$contents = '<span class="Menu1">Artur $2000</span> somehtml <span class="Menu1">Mark $1000</span>';
preg_match_all('@<span class="Menu1">(.*?)</span>@si', $contents, $wynik2);

print_r($wynik2);
/*
Array
(
    [0] => Array
        (
            [0] => <span class="Menu1">Artur $2000</span>
            [1] => <span class="Menu1">Mark $1000</span>
        )

    [1] => Array
        (
            [0] => Artur $2000
            [1] => Mark $1000
        )

)
*/

Upvotes: 6

Kannika
Kannika

Reputation: 2558

You should put this sign "|" in the start and the end of your regular expression :

preg_match_all("|<span class=\"Menu1\">(.*?)</span>|U",$contents,$wynik2);

Upvotes: 0

Related Questions