PHP capture word that contains special char from string using RegEx -



PHP capture word that contains special char from string using RegEx -

i have special words in string capture based on prefix. example special words such ^to_this should caught. need word this because of special prefix ^to_. here effort not working

preg_match('/\b(\w*^to_\w*)\b/', $str, $specialwordarr);

but returns empty array

your code be,

<?php $mystring = 'special words such ^to_this should caught'; $regex = '~[_^;]\w+[_^;](\w+)~'; if (preg_match($regex, $mystring, $m)) { $yourmatch = $m[1]; echo $yourmatch; } ?> //=>

explanation:

[_^;] add together special characters character class ensure begining of word special character. \w+ after special character, there must 1 or more word characters followed. [_^;] word characters must followed special character. (\w+) if these conditions satisfied, capture next 1 or more word characters group.

php regex

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -