Not Allowing Symbols In A String

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Not Allowing Symbols In A String

galexcd
I was looking for a quick solution so i could check a string for any symbols and stuff (basically anything that isnt numbers and letters) in it. One of the first google searches that came up were for a forum called "sitepoint". The thread was talking about the same thing I was trying to do.

This was their solution:
CODE
if (ereg("[a-zA-Z0-9]+", $username)) {
   echo 'OK.';
} else {
   echo 'Invalid characters.';
}


So i tried it out. Didn't work. I played with the code and i still couldn't get it to work... So I did what i didn't want to do: do it my way. Heres what i coded:
CODE
$allowed="q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m,Q,W,E,R,T,Y,U,I,O,P,A,S,
D,F,G,H,J,K,L,Z,X,C,V,B,N,M,1,2,3,4,5,6,7,8,9,0,-,_";
if(str_replace(explode(",",$allowed),"",$username)=="")
{
//OK
}else{
//contains symbols
}

Almost everything I code is very crude and I'm sure this could have been done with 1 simple function, but oh well. My way works fine.
---------
Heres the link to the thread if anybody is interested: http://www.sitepoint.com/forums/showthread.php?t=65200
--------
So why did i post this?
Basically The only reason i posted it here is for input. Even though i dont really need it now, anybody know why the original code didn't work? How about some input for the code i came up with! hehe rolleyes.gif . Maby sombody could use my code?
I donno. Since i just signed up to that forum just to make that one reply, I wont be visiting them again for a responce, so i just moved the whole issue over here, so I could get other people's oppinion.... Ok I think i should stop talking now.

 

 

 


Reply

truefusion
Well, the first code provided wouldn't have worked if there was an underscore, or a hyphen in the username. But it should have worked if it only contained letters and/or numbers.

But the simpler way to go about it for your second code would have been:
CODE
if (ereg('[\\w-]+', $username)) {
echo "OK";
} else {
echo "Invalid Characters, or no username provided.";
}

Reply

galexcd
QUOTE(truefusion @ Jul 26 2006, 10:31 AM) *

Well, the first code provided wouldn't have worked if there was an underscore, or a hyphen in the username. But it should have worked if it only contained letters and/or numbers.

But the simpler way to go about it for your second code would have been:
CODE
if (ereg('[\\w-]+', $username)) {
echo "OK";
} else {
echo "Invalid Characters, or no username provided.";
}



the first one din't work though, it was compleatly screwed up. $*%username said "invalid characters" but username$*% was allowed. Thats what made me finaly break down and drop the ereg function

Reply

Spectre
Try inversing the original regular expression, so that it scans for any characters which are not in the given set (in this case, anything that is not alphanumeric):

CODE
if (ereg('[^a-zA-Z0-9]', $username)) {
   echo 'Invalid characters.';
} else {
   echo 'OK.';
}


You can always add to the set to include other characters you want to allow, such as hyphens/dashes and underscores:

CODE
[^a-zA-Z0-9\-_]


The problem with the original expression is that it was matching any string that contained a consecutive set of alphanumeric characters at the start of the string. If any other characters followed, it simply ignored them, but still matched the expression.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : allowing, symbols, string

  1. Things To Know About Dreams
    Symbols, dream phonomenons, symbolic characters, kinds of dreams, colo (1)
  2. Unexpected T_string In User.php [resolved]
    (5)
    Ok so i'm working on a site for my chruch, and i seem to be having a little bit of trouble with
    the user.php file i keep getting the following error: CODE Parse error: syntax error,
    unexpected T_STRING in /home/darkzone/public_html/test/user.php on line 195 and i was wondering
    if some one could help me with the script. session_start(); ob_start(); //Include the
    configurations include('config.php'); //Define a few variables $x = $_GET ;
    $u = $_GET ; class register { function displayform($title) { echo('....
  3. Ms-access Database Question
    Allowing Web Access to the Informaton (3)
    Hi. I wanna know if there is a way of accessing an MS-Access database so that my site can extract
    data from it and make it avilable online. I have an accounting package that saves everthing in an
    MDB file and I want that info available for my clients from whereever thay are. Split Topic ....
  4. Adding String To Integer?
    (1)
    Create a PHP program with the following requirements  Create a program that does the following: 
    creates a variable called “test” (remember the dollar sign)  stores the number 35 to
    test and prints the result  adds 10.0000 to test and prints the result  subtracts 5.123123 from
    test and prints it out  stores the character string “happy to be here” to test and
    prints  adds 10.0000 to the variable test (try to guess what will happen) whats the solution to
    this problem? any help would be appreciated.....
  5. Data Structures -- String -- Arrange Based On Repetition
    String data structure (1)
    Consider a string with any number of elements occurring any number of time. Rearrange the string
    in such a way that the alphabet with most occurrence occurs in the followed by the next most
    occurring alphabet and so on... It should also be seen that the alphabets should occur frequency
    number of times. Example: Input : abcdaeghzabcdbhb Output : bbbbaaaccddhhegz ....
  6. Data Structures -- String -- Palindrome
    Check if a string is a palindrome... (5)
    Write an algorithm to check whether a given string is palindrome or not in time complexity O(n)
    What is a palindrome?? QUOTE A palindrome is a word, phrase, number or other sequence of units
    that has the property of reading the same in either direction (the adjustment of punctuation and
    spaces between words is generally permitted). Composing literature in palindromes is an example of
    constrained writing. The word "palindrome" was coined from Greek roots palin
    (πάλιν; "back") and dromos (δρóμος; "way,
    direction") by....
  7. T_string Error Please Assist
    (5)
    I have unexpected t_strings in 3 lines they are CODE <?php // $Header$ //this
    script may only be included - so its better to die if called directly. if
    (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !==
    false) {   header("location: index.php");   exit; } /* A basic library to
    handle a cache of some Tiki Objects, usage is simple and feel free to improve it */ class Cachelib
    {   var $folder;   function Cachelib() {         global $tikidomain;
            $th....
  8. <?php ?> Get Search Results To Your Localhost
    Simple way to get 100 results without allowing cookies (2)
    Ultimately simple script that allows you getting google search results to your localhost. This is
    the first part of the Crawler script i'm developing, and if you are interested in developing the
    script with me, IM me @icq328866661@msn/evil_matak/ a \hotmail-com. Here's the form
    part... QUOTE search.php QUOTE if (@$_POST ==""){ echo "What are you
    doing?"; } else{ $query =
    file_get_contents("http://www.google.com/search?q=".urlencode($_POST
    )."&num=100&hl=en&ie=UTF-8&filter=2"); //needs to be added with more queries �....
  9. <?php ?> Sloppy Login Script
    Sloppy login script, couse i used @ on one string (12)
    Here's a sloppy 3 files login script. First file is Login file that looks like this login.php
    CODE <form action="check.php" method="post"> Username: <input
    type="text" name="username1"><br /> Password: <input
    type="password" name="password1"><br /> <input type="submit"
    value="Login"> </form> Basicly that is HTML form that's used for input
    Second part of the script is the check.php that we call from our login.php form QUOTE ....
  10. Help Php: How To Load String From Text File (solved)
    Loading string from text file when you click on your link (9)
    I learned the way to load other files with the code posted on this forum. Now i wanted to try
    something for my side menu. I am calling this a string, --> $tekst , maybe it's
    called something else i'm not sure Now let's say i have a file called details.txt In that
    file i would like to have something like this $detailsaboutphp1 = ("details details details
    1"); $detailsaboutphp2 = ("details details details and even more details 2"); How to make a
    code that loads those $strings on click of a mouse. When we tried to load extern....
  11. Incrementing Mysql Integer
    int comes out as string? (7)
    I am attempting to perform mathematical operations on a number extracted from a MySQL database. The
    integer is stored as an integer in the database, and nothing I am doing to it in the PHP script
    would transform it into a floating-point value. I don't even want to worry about sticking it
    back in just yet: CODE // the value I want to increase here is logins... $query1 =
    "SELECT * FROM users WHERE id='$id'"; $result1 =
    mysql_query($query1) or die(mysql_error()); $row1 =
    mysql_fetch_array($....
  12. String Theory And The Ten Dimensions Of The Universe
    Advanced physics theories (10)
    String Theory and the Ten Dimensions of The Universe Recently I have found out about string
    theory. Most of the people on earth only knows that there only exist three dimensions(width, length,
    and height) and a fourth dimension which is the difference of a matter from one time point to
    another. And according to String Theory there exists six more spatial dimensions that are uknown or
    unnoticeable by a person. Basically, String Theory uses vibrating strings with a planck length,
    10^-35m, as a fundamental element of the universal not points. With this, physicists are a....
  13. String/text Formatting?
    (4)
    right i was wondering if someone could help me with this: I notice on this site if i make a topic
    for example WAppY rOCks WaP, it will come out like Wappy Rocks Wap. I also want to do this to my
    forums, can anyone give me the code to format a string of text in this way? THANKS IN ADVANCE ....
  14. Mysql For Vb.net
    Connection String (2)
    Put it simply, how could I connect MySQL into VB.NET? I use SQLConnection component, but it search
    only MS SQL servers. Please help... thanks in advance....
  15. Insert String To Mysql
    Important!!! (5)
    Hi everyone!

    I have completed the
    MySQL Table creation. but now I have a little larger problem /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" />
    ....
  16. Java String Overview
    (2)
    QUOTE String Overview Strings are sequences of Unicode characters. In many programming
    languages strings are are stored in arrays of characters. However, in Java strings are a separate
    object type, String. The "+" operator is used for concatenation, but all other operations on strings
    are done with methods in the String class. See the Summary - Strings for an overview of the methods
    in String and related classes. Related types and classes String The basic class for strings. String
    objects can NOT be changed. char Primitive type for 16-bit Unicode characters. Char....
  17. File String Delete?
    (2)
    Say i have a file file.txt... Can someone give me an example of how to delete a string from this
    file ie.. The file contains: wappy::sucks::at::php --- i want to delete sucks::....
  18. String Search Outside My Site?
    Help me work this out please ;-) (2)
    Hi ;-), i need some php code that will search any site or index page for a string. So i can check if
    my link is on sites it should be quickly?....
  19. Various String Functions In C
    (5)
    Hi, i read a lot of forum in which people want to learn C. Well i am giving this tutorial for all
    those who want to use strings. All the information provided is written by me and has not been copied
    from anywhere Well First of all lets see how to store a string into a string. First of all you
    need to initiate a variable which can hold an array of characters. It is necessary to initiate an
    array of characters rather than just a character: for example: CODE char string; is
    incorrect because if you store a string rather than a character in it, you cannot acces....
  20. Can I Cast A String As A Class Object?
    Using java.lang.Class (7)
    As you may or may not know, there's a java.lang.Class class which handles stuff that's
    pretty cool. You can use it to get the methods of a class, constructors of a class, etc. Anyway,
    I'm kind of interested in making a simple Java interpreter, and if I were able to cast a String
    that contains the name of a class into a Class object, it would make that project infinitely easier.
    The most obvious thing would be to use a Class object constructor, but there aren't any. So
    basically, I want to do something like this: CODE String s = "System"; Class ....
  21. Partial String Matching
    (1)
    Im trying to search a linked list to delete a record which is in a file that works. But im now
    trying to match only part of the string partial string matching. Im using strstr but i cant seem to
    get it working i get too few arguments error. And i also would like to know if im on the right track
    as to how im coding it. Any hints and tips would be greatly appreciated thanks. Code: CODE
    /**************************************************************************** * Menu option #3:
    Delete Record * Allows the user to remove one or more records from the customer and....
  22. Building My First Instrument
    4-string fretless bass (6)
    This project has been my main passion for quite a while now, but because of a lack of funds, it has
    been on standby for a while. It will be the first instrument I've ever built, aswell as my first
    real experience with woodworking. The neck seen in the picture below is not the neck that will be in
    the final product. That neck managed to warp on me, so I'm just using a single board of wenge
    instead. Here are some specs: Strings: 4 Fretted/Fretless: Unlined Fretless Body:
    Maple/Walnut/Maple Neck: Wenge with Mahogany and Poplar at Body Neck Construction: Through....
  23. Determining Whether A Phrase Is In A Variable
    deciding whether a string is a url (2)
    I have a input box in a form called "button" and when the user clicks Submit! I want php to
    determine whether or not this is a url or just text. All urls have "http://" in them so I have
    decided I need a script which can determine whether or not "http://" is in a variable or a string.
    The problem with using strings is that I've never needed to use them before (and thus have no
    knowledge about them). When I was using asp, I knew there was a function called instr() but I
    can't find php's equivalent. Does anybody know? Thanks in advance!....
  24. Allowing Everyone To Post To Phpbb
    Can this be done? (6)
    I was wondering, is there any way that allows anyone to post to phpbb? Even all the guests that come
    to the board? Is this even a good idea? I'm not sure what kind of things would happen if I did
    that. Like would I be facing lots of spam, or is there something that I should be worried about if
    I'm going to run my boards that way. Does anyone else have a board open to anyone or have you
    tried that before? And if so, how have you found it? I'd like to know what to expect a little
    more if I'm going to go about doing that sort of thing. I think it is the be....
  25. A Function To Count The Number Of Lines In A String?
    (4)
    I'm making a forum and I'm currently coding signatures. Is there a function that I can use
    to make sure that what the user inputs is a certain number of lines?....
  26. What Is A T_string?
    Please help! I'm new at PHP Programming (13)
    I'm new at PHP, and I looked for a while on here, and found some stuff I was looking for about
    form processing, and sending E-Mail through PHP. However, the script I just created won't work.
    I get the following error: Parse error: parse error, unexpected T_STRING in
    /home/hamtaro/public_html/send_feedback.php on line 9 Exactly what is a T_STRING? If I need to post
    my code, I will.....
  27. String <-> ANSIChar
    (1)
    Who knows how can I change String nto AnsiChar??....

    1. Looking for allowing, symbols, string

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for allowing, symbols, string

*MORE FROM TRAP17.COM*
Similar
Things To Know About Dreams - Symbols, dream phonomenons, symbolic characters, kinds of dreams, colo
Unexpected T_string In User.php [resolved]
Ms-access Database Question - Allowing Web Access to the Informaton
Adding String To Integer?
Data Structures -- String -- Arrange Based On Repetition - String data structure
Data Structures -- String -- Palindrome - Check if a string is a palindrome...
T_string Error Please Assist
<?php ?> Get Search Results To Your Localhost - Simple way to get 100 results without allowing cookies
<?php ?> Sloppy Login Script - Sloppy login script, couse i used @ on one string
Help Php: How To Load String From Text File (solved) - Loading string from text file when you click on your link
Incrementing Mysql Integer - int comes out as string?
String Theory And The Ten Dimensions Of The Universe - Advanced physics theories
String/text Formatting?
Mysql For Vb.net - Connection String
Insert String To Mysql - Important!!!
Java String Overview
File String Delete?
String Search Outside My Site? - Help me work this out please ;-)
Various String Functions In C
Can I Cast A String As A Class Object? - Using java.lang.Class
Partial String Matching
Building My First Instrument - 4-string fretless bass
Determining Whether A Phrase Is In A Variable - deciding whether a string is a url
Allowing Everyone To Post To Phpbb - Can this be done?
A Function To Count The Number Of Lines In A String?
What Is A T_string? - Please help! I'm new at PHP Programming
String <-> ANSIChar
advertisement



Not Allowing Symbols In A String



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE