feri_soft Posted October 17, 2006 Share Posted October 17, 2006 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 :) Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/ Share on other sites More sharing options...
alpine Posted October 17, 2006 Share Posted October 17, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-110188 Share on other sites More sharing options...
feri_soft Posted October 17, 2006 Author Share Posted October 17, 2006 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 Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-110212 Share on other sites More sharing options...
Ninjakreborn Posted October 17, 2006 Share Posted October 17, 2006 regular expressions. Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-110215 Share on other sites More sharing options...
Barand Posted October 17, 2006 Share Posted October 17, 2006 [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 knowEDIT: but how do you know "asdasdasdasdasdasdasdasd" is wrong? - the user may just want to enter a single item Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-110240 Share on other sites More sharing options...
feri_soft Posted October 18, 2006 Author Share Posted October 18, 2006 well,the minimul will be 2 ;) Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-110811 Share on other sites More sharing options...
feri_soft Posted October 20, 2006 Author Share Posted October 20, 2006 Ideas?? Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-111938 Share on other sites More sharing options...
Orio Posted October 20, 2006 Share Posted October 20, 2006 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. Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-112000 Share on other sites More sharing options...
kenrbnsn Posted October 20, 2006 Share Posted October 20, 2006 You could use the [url=http://www.php.net/strpos]strpos()[/url] function:[code]<?phpif (strpos($_GET['s'],',') === false) echo "no comma found";?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-112004 Share on other sites More sharing options...
Psycho Posted October 20, 2006 Share Posted October 20, 2006 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. Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-112049 Share on other sites More sharing options...
feri_soft Posted October 22, 2006 Author Share Posted October 22, 2006 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. Link to comment https://forums.phpfreaks.com/topic/24245-comma-seperated-input/#findComment-112608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.