gongpex Posted October 28, 2019 Share Posted October 28, 2019 Hello everyone, I have code: $a = ' asasc #aaa cscv #ccc'; if (preg_match('/\#(\w+)/',$a, $matches)) { print_r($matches); } this result are : Array ( [0] => #aaa [1] => aaa ) that result is not my expectation, my expectation if the result : Array ( [0] => #aaa [1] => #ccc ) Q : So in other word how to make so that preg_match return only the word start with # only. please someone help me Thanks Link to comment Share on other sites More sharing options...
dsonesuk Posted October 28, 2019 Share Posted October 28, 2019 (edited) <?php $a = explode(' ','asasc #aaa cscv #ccc'); $c=[]; foreach($a as $b){ if (preg_match('/\#(\w+)/',$b,$matches)) { $c[]= $matches[0]; } } print_r($c); Edited October 28, 2019 by dsonesuk Link to comment Share on other sites More sharing options...
dsonesuk Posted October 29, 2019 Share Posted October 29, 2019 (edited) $matches always return 2 array elements, first with whole string with searched for value, the second without. You have gather the first then store it while looping through all individual words added into array using explode(). Edited October 29, 2019 by dsonesuk Link to comment Share on other sites More sharing options...
robsstevejobs Posted September 18, 2020 Share Posted September 18, 2020 Hello, How I can filter the password with the function preg_mettre by specifying optional special charater but the first letter is mandatory uppercase is the rest or lowercase. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now