Forum Moderators: coopster

Message Too Old, No Replies

regex for comma separated str

regards tagging

         

henry0

12:33 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am working on a tagging system
It gets the values from $_POST
Then use explode() etc..
But something occurred in my mind:

The user must enter comma separated values

Obviously if a comma is forgotten but a space between two words exists, then one of the tags will become a combo of two words; therefore useless!

So I need to check if:
Beginning and end of str does not have a comma. (why not checking as well for such an error)
And that anywhere in the str if a space exists a comma separating two words has been entered if not send an exception

This form of regular expression is way above my capability, it needs to factor that although we know the str allowed length, we could not know how many tags could be submitted.
Could you help? Thanks

Let’s make a deal you help with the regex thing, and then I will post the whole script when finished and tested :)

henry0

1:49 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I kind of reversed the process
by asking to enter tags space separated
and performing a bunch of actions
I may digest the example and turn it in a function
as is it works but looks "heavy"
I'll rather go with the regex as above defined

<?php
$_POST['fields']="aaa bbbb cccc";
$fields=$_POST['fields'];

// check if any comma exists
// if exists then del it
if (!preg_match("/^[,]+$/",$fields))
{
$fields = str_replace(",", "", $fields);
}
// make sure that no space exists left and right of str
$fields=ltrim($fields);
$fields=rtrim($fields);

// replace spaces with a comma

$fields = str_replace(" ", ", ", $fields);
$fields=explode(" ",$fields);
print_r($fields);
?>

out of curiosity I checked a bunch of tagging scripts and tutorials; strange none do care checking for a missing comma or space

RonPK

2:03 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$fields = preg_split("/[,\s]/", $fields);
// splits the string on comma or whitespace. returns an array.

But: say someone posts 'green widgets'. He or she might actually want 'green widgets' to become a tag, and not 'green' and 'widgets'.

henry0

2:30 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks
you have a very valid point
so it really needs to be comma separated
but then it throws a wrench in my logic

how figuring if she/he wants a "tag combo" among single words combo

possibly adding an ajax to show their intent
"do you really want ...."

RonPK

3:08 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd simply do what everybody does: expect a comma separated list. Show the user a textinput with explanation:

Hi, please enter some relevant tags for the article! Please use a comma to delimit tags (e.g. "Henry VIII, London").
<input type="text" ...>

Another solution might be to use a textarea, and have the user enter each tag on its own line. Split the posted string on the newline character.

eelixduppy

4:04 pm on Jul 25, 2008 (gmt 0)




Split the posted string on the newline character.

This has more to it than meets the eye. Sometimes the OS terminates the textareas with \r only, and sometimes with \r\n or even just \n. Coopster provided a nice solution for this:


$data = [url=http://www.php.net/array-map]array_map[/url]('trim', [url=http://www.php.net/preg-split]preg_split[/url]("/\r\n¦\n¦\r/", $text, -1, PREG_SPLIT_NO_EMPTY));

Thanks coop ;)

henry0

4:41 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, coop! thanks, eelix!

OK you'll get your tags tutorial :)
(sometimes by last week of 08, I am now due to travel abroad starting 07/31)

henry0

7:02 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



allready hitting a snag:)
my purpose

Project at hand
I made a “ask a question” with a rating system for each answers
Each question is posted with its own set of tags
(so far so good)

Next:
To simplify my definition let’s say that I need to create a DD box made out each unique tags (I pass on pre-tags-screening)

But I am hitting a snag at the insert level (I guess)
How inserting in DB $data?
//$data = array_map('trim', preg_split("/\r\n¦\n¦\r/", $text, -1, PREG_SPLIT_NO_EMPTY));

I tried
A for($i etc...
Tried to implode it but in enters the row as a str

then to retrieve the values and pass them in a DD box
I need to make sure the values are unique (that's easy) but the DD box echoed a multiple copy of each row in direct proportion to total num rows
Plus it displays the whole tag str

Ex
DB Row1= aaa ccc vvvv
DB Row2= sdsd qwqw

Then the DD box reads
aaa ccc vvvv
aaa ccc vvvv
sdsd qwqw
sdsd qwqw

I should get:
aaa
cccc
vvvvv
sdsd
qwqw

so what should be the best way to enter $data in DB and retrieve its value
thanks

henry0

7:22 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



presently trying serialise ans unserialize

henry0

7:34 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



moving a tad ahead

OK it's serialized in DB
Next to get the result I use

////// ajax call:
while ($row=$db->fetch_array($result_tags))
{

$tags=unserialize($row['tags']);
$i_tags=0;
echo"<form><select name='tag_search' onchange='pass_val(this.value)'>";
echo"<option value=''>Select from list";
while($i_tags <$num_tags)
{
$tags=mysql_result($result_tags, $i_tags, "tags");
echo"<option value=$tags>$tags";

$i_tags++; echo'</option>';
}
}
echo"</select></form><div id=\"show\"></div>";

with a result of
Notice: unserialize() [function.unserialize]: Error at offset 11 of 44 bytes in C:\....... etc etc on line 99

and the DD box is populated with the full content from the DB:
a:1:{i:0;s:31:"asasas xcxc asasas
asasas";}

henry0

7:42 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just found that the serialized values are not
really serialized
this looks wrong, each value is not "serialized"
a:1:{i:0;s:31:"asasas xcxc asasas
asasas";}
so we are back on how to enter properly the serialized $data