-1

I'd love some help with this small problem.

I need PHP to collect HTML. Lets say, this is a part of the full HTML code:

<div class="inner">
                <p>Hi there. I am text! I'm playing hide and seek with PHP.</p>
            </div>

My goal is to collect everything between <p> and </p>. This is the PHP I've got so far:

    $file = file_get_contents($link); //Import le HTML
    preg_match('<div class="inner">
                        <p>(.*?)</p>
                    </div>si', $file, $k); //Play find & seek
    $k_out = $k[1];
    $name = strtok($k , '#'); //Remove everything behind the hashtags

    echo $name;

But - sadly - PHP error'd me:

*Warning: preg_match(): Unknown modifier '<' in /home/fourwonders/alexstuff/vinedownloader/public_html/v/index.php on line 131*

Can you help me out? At least, thanks for reading!

  • 2
    You shouldn't parse HTML with regular expressions, when you use [DOMDocument](http://php.net/domdocument) e.g. you can easily access the wanted element – kero Sep 21 '13 at 16:28

2 Answers2

2

In this case it's because you don't specify delimiters (you always need delimiters, and you need to always escape the delimiter character if it is in your expression:

preg_match('#<div class="inner">
                    <p>(.*?)</p>
                </div>#si', $file, $k);
Nicole
  • 32,841
  • 11
  • 75
  • 101
2

Don't use regular expressions to parse HTML. Use a DOM Parser instead:

$doc = new DOMDocument();
$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('p');
foreach ($tags as $tag) {
    echo $tag->nodeValue;
}

Output:

Hi there. I am text! I'm playing hide and seek with PHP.

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150