简体   繁体   中英

a regular expression in preg_match that returns 3 matches

My input is as below:

*test*

The output I want is content inside two asterisks (test).
My code is as below:

preg_match('/^(\*(.*)\*)$/','*test*',$matches);

Its output is:

 Array ( [0] => *test* [1] => *test* [2] => test ) 

The third one is the one I want. I know why it does this, but I don't know how to solve it. How to write an RE that returns just test nothing else.

您可以使用前瞻和后瞻断言

/(?<=^\*).*(?=\*$)/

you can try this by using explode function :

<?php $str = '*test*';
$str1 = explode('*',  $str);
echo $str1[1]; 
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM