Defense
Defense

Reputation: 259

Regular expression for span tag

How to regex span tag exp.

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

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

Upvotes: 0

Views: 1507

Answers (1)

Nilpo
Nilpo

Reputation: 4816

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

Upvotes: 1

Related Questions