idm :
I have this array, that I need to remove white spaces and # hashtag character:
array (size=7)
0 => string 'darwin' (length=6)
1 => string ' #nature' (length=8)
2 => string ' explore' (length=8)
3 => string ' galapagos' (length=10)
4 => string 'karma' (length=5)
foreach ($feedSinglePosts["hashtags_list"] as $key=>&$item) {
$item = preg_replace('/(\s|^)/', '', $item);
$item = preg_replace('/\#+/', '', $item);
}
The Regex above works well but I want to make it one line if possible. When I do: /(\s|^)\#+/
it outputs this:
array (size=7)
0 => string 'darwin' (length=6)
1 => string 'nature' (length=6)
2 => string ' explore' (length=8)
3 => string ' galapagos' (length=10)
4 => string 'karma' (length=5)
How to make the regex on liner that removes white spaces and3 hashtag.
AbraCadaver :
It appears that the characters will be at the beginning or end. If so then no need for loops or regex:
array_walk($feedSinglePosts["hashtags_list"], function(&$v) { $v = trim($v, "\n\r #"); });
If you need to remove them anywhere:
$feedSinglePosts["hashtags_list"] = str_replace(["\n","\r"," ","#"], "", $feedSinglePosts["hashtags_list"]);