runeveryday
runeveryday

Reputation: 2799

what are these regex expressions meaning?

  1. preg_match( '/<title>(.*)<\/title>/',.....)

  2. preg_match("/src=[\"']?([^\"']?.*(png|jpg|gif))[\"']?/i",....)

Upvotes: 0

Views: 215

Answers (2)

Pekka
Pekka

Reputation: 449633

The first is to extract the contents from a HTML title tag.

The second is to extract images' src attributes from a HTML document, but is very imperfect (It won't catch references to image resources that end in .jpeg or have no extension at all).

Regular expressions are not a good idea for parsing HTML! One should use a HTML parser instead. They are far from fireproof.

Upvotes: 6

beggs
beggs

Reputation: 4195

1) Matches anything between <title> and </title> a la an HTML page's title, so run against <title>foo</title> results in the match being foo.

2) Matches any string following src= that ends in png, jpg or gif. Used to extract the URL of images in HTML code.

Per @Pekka's answer: don't do this in real world code.

Upvotes: 0

Related Questions