Jump to content

matching multiple words in rexep


grunch

Recommended Posts

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

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);

?>

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);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.