views:

202

answers:

6

I have a tag field in my web page, in which user can enter tags seprating them by , (comma), same as done in StackOverflow. I am using PHP and I am seprating all tags enetered by the user on the basis of comma using explode function and then adding them to my tags table in teh database.

My code is working perfect for normal tags like if user entered battleship, strategy, sea war then its working, but lets say user enetered battleship, strategy, sea war, or battleship, strategy, games, sea war, , , , , or battleship, strategy, games,, sea war or any wrong value, then how will I detect it and then enter only correct value sin the database.

Is it possible using regExp or any other way, please tell me how?

+1  A: 

You can use regular expressions to split the string into the tags, for example:

\s*,\s*

$tags = preg_split('/\\s*,\\s*/', $str);
$tags = array_filter(array_map('trim', $tags));
var_dump($tags);

But then you still need to check if the tags are valid. An empty tag for example (like in foo,,bar) isn’t. Just filter them out and pass the rest to your database.

Or you use a regular expression to just find valid tags:

\w+(?:\s+\w+)*

preg_match_all('/\\w+(?:\\s+\\w+)*/', $str, $tags, PREG_PATTERN_ORDER);
var_dump($tags);
Gumbo
I think that is why SO is using spaces to separate tags, because if someone enters double spaces or single spaces in the last then we can trim it easily. ?
Prashant
You can do that with commas and spaces too: `trim($str, ', ')`.
Gumbo
+6  A: 
$tags = "battleship, stragety, ,";

$exp = explode(",", $tags);

$valid_tags = array();

foreach($exp as $tag)
{
 if(trim($tag) != "")
 {
   $valid_tags[] = $tag;
 }
}

Will remove whitespace from any tag and if it has any text, tag will be added into valid array :)

usoban
A: 

Better way is to suggest user what tags they can add - easy you can do it whit jquery ajax :) Some think like here (ajaxdaddy.com)

lfx
+1  A: 

No need for RegExps in this case. explode delivers the tag snippets, and trim removes white spaces. Just check, if then is something left.

$tags = explode (",", $posted_tags);
foreach ($tags as $tag) {
    if ($value = trim ($tag)) {
        insert_into_db ($value);
    }
}

Cheers,

Boldewyn
+1  A: 
$str = "ship,, , , , ,water";

$tags = explode(',', $str);

foreach($tags as $tag) {
    if( $tag ) {
     echo trim($tag) ;
    }
}
lyrae
you may want to trim tags before inserting, in case somebody enters "ship, battle, naval"
Kuroki Kaze
Thanks kuroki. I edited.
lyrae
I'm not quite sure, what the isset() does. $tag is set anyways, even if it is the empty string.
Boldewyn
thanks. isset wasn't really needed, it's just that i'm in the habit of overusing it
lyrae
A: 

You can probably get away with the very succinct:

$tags = array_filter(explode(',', $str));

since (from the manual for array_filter):

If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.

(That's if you don't mind rejecting '0', and other values that evaluate to FALSE)

Bobby Jack