Jump to content


Photo

Comma seperated input


  • Please log in to reply
10 replies to this topic

#1 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 17 October 2006 - 06:13 PM

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 :)

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 17 October 2006 - 06:35 PM

Here is one way, i assume you want to split the string if it is comma-separated:

<?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>";
}

?>



#3 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 17 October 2006 - 07:09 PM

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

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 17 October 2006 - 07:11 PM

regular expressions.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 October 2006 - 08:01 PM

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


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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 18 October 2006 - 06:10 PM

well,the minimul will be 2 ;)

#7 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 20 October 2006 - 04:50 PM

Ideas??

#8 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 20 October 2006 - 06:28 PM

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.
Think you're smarty?

(Gone until 20 to November)

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 October 2006 - 06:46 PM

You could use the strpos() function:
<?php
if (strpos($_GET['s'],',') === false) echo "no comma found";
?>

Ken

#10 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 20 October 2006 - 08:29 PM

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.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#11 feri_soft

feri_soft
  • Members
  • PipPipPip
  • Advanced Member
  • 147 posts

Posted 22 October 2006 - 08:59 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users