How to select a word from a string containing a specific character or symbol in PHP? -


i want usernames post , send them notification.

the post may this.

congratulations  @user1  @user2  @user3  have won! 

i want select words above string contains @ symbol , store them elements of array can use them individually send notification them.

i using following code gives on 1 user. how rest of them?

<?php $post = "congratulations  @user1 @user2 @user2 have won!"; if (strpos($post, "@")) { $fetch = explode("@", $post); $show = explode("\r\n", $fetch[1]); $users = count($show);  foreach ($show $value) {     echo "$value <br>"; } echo "<br>total $users users."; } ?> 

update:

i tried users suggested @pharaoh

<?php $post = "congratulations  @user1 @user2 @user2 have won!"; if (strpos($post, "@")) { $matches = []; preg_match_all('/@(\w+)/', $post, $matches); $users = count($matches); $matches = array_values($matches); foreach ($matches $value) {     echo "$value <br>"; } echo "<br>total $users users."; } ?> 

it gives 2 arrays output below.

array  array   total 2 users. 

what doing wrong?

i regular expressions:

$matches = []; preg_match_all('/@(\w+)/', $post, $matches); var_dump($matches);  

that way, usernames don't need @ start of line:

this sentence @user1 in middle. 

you can see in action here: https://eval.in/847703
(in case of error, reload. eval.in seems have problems)


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -