Jump to content

Archived

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

feri_soft

Comma seperated input

Recommended Posts

How to check if the input words are seperated by comma.For example i have a field and i want to enter some keywords.How to check if they are seperated by comma.I know that it must be done with regular expresions but i dont know how to use them exactly.Will check the php manual,but if you have any suggestions please help :)

Share this post


Link to post
Share on other sites
Here is one way, i assume you want to split the string if it is comma-separated:
[code]

<?php

$s = $_GET['s']; // search string or whatever

$string = explode(",",$s);
if(count($string)>1)
{
  echo "comma exists in <b>$s</b>, separated as:<br /><pre>";
  print_r($string);
  echo "</pre>";
}
else
{
  echo "no commas found in <b>$s</b>";
}

?>

[/code]

Share this post


Link to post
Share on other sites
No,i dont want to split it i just want to be shure that the user adds to the db something like

asdasd,asdasd,asdasd,asdas not asdasd-asdasd-asdasd or sdasdasd/asdasdasd/asdasd or asdasdasdasdasdasdasdasd

Share this post


Link to post
Share on other sites
[quote author=feri_soft link=topic=111808.msg453303#msg453303 date=1161112159]
No,i dont want to split it i just want to be shure that the user adds to the db something like

asdasd,asdasd,asdasd,asdas not asdasd-asdasd-asdasd or sdasdasd/asdasdasd/asdasd or asdasdasdasdasdasdasdasd
[/quote]

Even if you don't want the split results, Alpine's code will still tell you what you want to know

EDIT: but how do you know "asdasdasdasdasdasdasdasd" is wrong? - the user may just want to enter a single item

Share this post


Link to post
Share on other sites
alpine's code does the work for you.
These variables are temporary, just continue to work inside the if or something with the unsplitted variable.

Orio.

Share this post


Link to post
Share on other sites
You could use the [url=http://www.php.net/strpos]strpos()[/url] function:
[code]<?php
if (strpos($_GET['s'],',') === false) echo "no comma found";
?>[/code]

Ken

Share this post


Link to post
Share on other sites
Can the data values include non alphanumeric characters? If not just use regular expressions to replace non-alphanumeric characters to commas. however, if the users can enter data value with non-alphanumeric characters, then you cannot do what you are asking because you wouldn't be able to determine if the user is using "-" as a delimiter or as part of the data.

Share this post


Link to post
Share on other sites
As the string will be with keywords i dont think someone will use - ot ! or ? etc. as keyword.
But yes it will be helpful to put a regular expression to allow only letters.

Share this post


Link to post
Share on other sites

×

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.