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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.