2
$var = '<lang test=<php>string</php>><lang test2=before<php>inside</php>>'.
       '<lang test3=THIRD_WITHOUT_PHP_TAGS><lang test4>';
if (preg_match_all('#<lang (.*?)>#', $var, $gg))
{
    var_dump($gg[1]);
}

I get the dump:

array(2) {
  [0]=>
  string(9) "test=<php"
  [1]=>
  string(16) "test2=before<php"
}

But I want to get :

test=<php>string</php>

and

test2=before<php>inside</php>

and

test3=THIRD_WITHOUT_PHP_TAGS

and

test4

How to do it?

EDIT: I CHANGE THE $var AND ADD third expression

EDIT 2: ADEED the fourth expression without "="

3
  • Is </php> always at the end (</php>>) ? Commented Nov 26, 2010 at 12:46
  • you could use $var = '<lang test=[php]string[/php]><lang test2=before[php]inside[/php]>'; and you whont need to modify you're regexp , or you could skip the first two ocurences of '>' but then what if you pass $var = '<lang test=some other test> without any <php> tags ? somebody else better than me will figure out an answer without the need to change the php tags Commented Nov 26, 2010 at 12:48
  • @Jan Hančič, not always. This may not be Commented Nov 26, 2010 at 12:48

3 Answers 3

3

The easiest way would be

#<lang (\w+=[^<]*(<[^>]+>)?[^<]+(?(2)</[^>]+>))>#

The Regex checks if a tag is matched and only then checks for ending tag.

If you want to catch also the fourth argument, you need to use

#<lang (\w+(=[^<]*(<[^>]+>)?[^<]+(?(3)</[^>]+>))?)>#
Sign up to request clarification or add additional context in comments.

3 Comments

EDIT: I CHANGE THE $var AND ADD third expression
Should work aswell. I just tried it. It matches the first two and doesn't get the third lang.
Thank you.. but I have a problem.. <lang test4> .... test4 It is necessary that "test4" too was in the array.. For your tests: $var = '<lang test=<php>string</php>><lang test2=before<php>inside</php>><lang test3=int><lang test4>';
0
$var = '<lang test=<php>string</php>><lang test2=before<php>inside</php>>';
if (preg_match_all('#<lang (.*>)>#U', $var, $gg))
{
    var_dump($gg[1]);
}

Comments

-1

I would do it this way:

preg_match("/<lang (.*)>/i", $var, $gg)

3 Comments

This would completly match <lang test=<php>string</php>><lang test2=before<php>inside</php>><lang test3=THIRD_WITHOUT_PHP_TAGS>
@ S-Buzz but he says he wants to get all so why the downrep?
He wants to get all three ones as a single match. Your regex matches the whole string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.