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 Link to comment https://forums.phpfreaks.com/topic/94046-matching-multiple-words-in-rexep/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/94046-matching-multiple-words-in-rexep/#findComment-481786 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); Link to comment https://forums.phpfreaks.com/topic/94046-matching-multiple-words-in-rexep/#findComment-481789 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 Link to comment https://forums.phpfreaks.com/topic/94046-matching-multiple-words-in-rexep/#findComment-481804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.