-2

How do I find these and other options:

<title itemprop="name">Title</title>
<title>Title</title>

with this

preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );

Thanks!

Vasily
  • 1,858
  • 1
  • 21
  • 34
topedge
  • 73
  • 1
  • 9

1 Answers1

2

If you want to get all the titles then you'll probably want to use preg_match_all nd not preg_match. You should also take into consideration that there might be extra data (such as attributes) right after the <title. Lastly, your "greater-than" sign should be changed to a "greater-than" - you're looking until a new tag is begining.

Here's a working solution:

preg_match_all('/<title[^>]*>([^<]*)<\/title>/si', $contents, $match );
uri2x
  • 3,162
  • 1
  • 11
  • 25