Forum Moderators: coopster
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 :)
<?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
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.
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 ;)
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
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";}