0

How to regex span tag exp.

<span id="n1">value here</span>

I need to get span id and "value here"?

Musa
  • 96,336
  • 17
  • 118
  • 137
Defense
  • 259
  • 4
  • 21
  • 1
    You shouldn't use regex to parse HTML. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Nilpo Aug 25 '12 at 23:29

1 Answers1

1

You shouldn't use regex to parse HTML. However, if you must, this should do it.

$regex = '#<span id="(.+?)">(.+?)</span>#';
preg_match($regex, $input, $groups);

$id = $groups[1];
$value = $groups[2];

This is in no ways absolutely bulletproof. Not by a long shot.

You should really take a look at something like DOMDocument

Nilpo
  • 4,675
  • 1
  • 25
  • 39