Dzhuneyt
Dzhuneyt

Reputation: 8701

Get Exactly 2 Characters Using Preg_match

I am having trouble with this expression:

preg_match('#<file(.*)source-language="(.){2}"(.*)>#Ui', $xliff, $matches);

I am trying to get the 'en' out of (exactly 2 a-z characters can be there):

<file src="index.php" source-language="en" date="2012-10-10">

Upvotes: 1

Views: 813

Answers (3)

Explosion Pills
Explosion Pills

Reputation: 191749

preg_match('#<file(.*)source-language="(..)"(.*)>#Ui', $xliff, $matches);

I think this is a simpler way if you know this is going to be exactly two characters. It could be more, though, so be careful.

You weren't capturing both characters before because the {2} was outside the capture.

Upvotes: 2

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5626

Parsing HTML with regular expressions is a bad idea. Try Document Object Model instead.

Upvotes: 0

kingcoyote
kingcoyote

Reputation: 1146

You've got the quantifier outside the capture section. You need to move the {2} inside the () so that both get captured.

'#<file(.*)source-language="(.{2})"(.*)>#Ui'

Upvotes: 3

Related Questions