grunch Posted March 2, 2008 Share Posted March 2, 2008 Hi everybody, im having a little problem with rexep in PHP, the problem is: i have this string var $string = "campo01, campo02, campo03, campo04, campo05,"; but the string also can be like this: $string = " campo01 ,campo02, campo03,campo04, campo05, "; i have to match the string with a rexep and group the words campoX in a array i did this: if(ereg("^([[:blank:]]*[a-zA-Z0-9_]+[[:blank:]]*,)+", $string, $a){ // code } but is not working, how i can make this work? thx in advance Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 2, 2008 Share Posted March 2, 2008 You would be better off using preg_split(); http://us2.php.net/manual/en/function.preg-split.php <?php $string = " campo01 ,campo02, campo03,campo04, campo05, "; $array = preg_split('/\s*,\s*/', $string, NULL, PREG_SPLIT_NO_EMPTY); ?> Quote Link to comment Share on other sites More sharing options...
dsaba Posted March 2, 2008 Share Posted March 2, 2008 Not too familiar with ereg syntax but try this: $pat = '[[:blank:]]*[a-zA-Z0-9_]+[[:blank:]]*,'; --------------------------------------------------------------- If you use preg_match_all() its pretty easy: $pat = '~\w+~'; or $pat = '~\s*(\w+)\s*,~'; preg_match_all($pat, $haystack, $matchesArr); print_r($matchesArr); Quote Link to comment Share on other sites More sharing options...
grunch Posted March 2, 2008 Author Share Posted March 2, 2008 fnairb and dsaba thank you very much, both help me a lot greets Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.