开发者

PHP preg_match regular expression

开发者 https://www.devze.com 2023-01-27 06:29 出处:网络
$var = \'<lang test=<php>string</php>><lang test2=before<php>inside</php>>\'.
$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 "="


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)</[^>]+>))?)>#


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


I would do it this way:

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

精彩评论

暂无评论...
验证码 换一张
取 消