how to test string whether it contains special characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • exoskeleton
    New Member
    • Sep 2006
    • 104

    how to test string whether it contains special characters

    please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid!

    please help...TIA
  • devsusen
    New Member
    • Feb 2007
    • 136

    #2
    I prefer preg_match() in such cases. Can u tell us what sort of special chars u like to search within names?

    susen

    Comment

    • tolkienarda
      Contributor
      • Dec 2006
      • 316

      #3
      Originally posted by exoskeleton
      please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid!

      please help...TIA
      one simple way to do that is with the strpos() function. here is a short tutorial of how it works
      Code:
      $username = 'frank!anderson';
      $pos = strpos($username, "!");
      if ($pos == -1)
      {
         print 'dont put ! in your usernames you silly person';
      }
      else
      {
         print 'everything is super cool and what not';
      }
      i hope that helps other functions to look at include
      strcmp()
      strstr()
      documentation can be found at php.net/function name
      ex


      eric

      oh and devsusen is right pregmatch is probably the most standard way to this. but is far to complicated for a simple mind such as myself so i always use these dumbed down versions.

      Comment

      • exoskeleton
        New Member
        • Sep 2006
        • 104

        #4
        Originally posted by devsusen
        I prefer preg_match() in such cases. Can u tell us what sort of special chars u like to search within names?

        susen
        thank you sir devsusen.. :) but how can i add characters to preg_match() in order to find not just 1 special characters like the split function that only enclosed by "[ ]" like

        Code:
        split('[/.-]', $date);
        in order to find not just 1 character in an expression or given string?

        i tried like for example:

        Code:
        $string = '#richa@rd';
        preg_match('/!@#$%^&*()/','$string');
        but cant able to find anything....and he can find something if like this:

        Code:
        $string = 'richard';
        preg_match('/chard/','$string');

        Comment

        • exoskeleton
          New Member
          • Sep 2006
          • 104

          #5
          Originally posted by tolkienarda
          one simple way to do that is with the strpos() function. here is a short tutorial of how it works
          Code:
          $username = 'frank!anderson';
          $pos = strpos($username, "!");
          if ($pos == -1)
          {
             print 'dont put ! in your usernames you silly person';
          }
          else
          {
             print 'everything is super cool and what not';
          }
          i hope that helps other functions to look at include
          strcmp()
          strstr()
          documentation can be found at php.net/function name
          ex


          eric

          oh and devsusen is right pregmatch is probably the most standard way to this. but is far to complicated for a simple mind such as myself so i always use these dumbed down versions.
          same with this sir... how can i add special characters to find not just 1.. yes this is possible if only 1 special character to find...

          thank you sir

          Comment

          • tolkienarda
            Contributor
            • Dec 2006
            • 316

            #6
            well my advice would be to go back to devsusen's idea. and what preg_match as i am sure you know uses regular expressions and a good tutorial on the complex world of regex is

            and there are many more but this is php based and a good starting place
            unfortunatly the functions i listed earlier can only look for one expression at a time. if you had an array of strings you were looking for say

            you can use a loop
            Code:
            $string = 'adsf@ASDF!fdsa#FDSA%';
            $lookfor[0] = '!'
            $lookfor[1] = '@'
            $lookfor[2] = '#'
            $lookfor[3] = '%'
            $i = 0;
            while ($i < 4)
            {
               $var = strpos($string, $lookfor[$i]);
               if ($var != -1)
               {
                  echo 'passed test for $lookfor[$i]';
               }else
               {
                   echo 'silly person none of those silly charcters';
               }
            
            }
            now i typed this code into this forum directly so there might be a missing semi colon or paren but the basic logic will work.
            (preg_match and regular expressions i have avoided for my entire programing carear because the confuse me to no end but if you can figure them out i urge you to go in that direction because they are extreamley powerful and i always feel stupid when i have a problem that could be easily solved with regex but i cannot do it without loops and if statements)

            eric

            Comment

            • exoskeleton
              New Member
              • Sep 2006
              • 104

              #7
              thank you sir... :)

              Comment

              • devsusen
                New Member
                • Feb 2007
                • 136

                #8
                hi,

                Originally posted by exoskeleton
                how can i add characters to preg_match() in order to find not just 1 special characters
                To check multiple special characters u can use preg_match too. There u need to specify the characters u like to check using separator. I'm giving u an example.

                Code:
                preg_match('/[\s|\n|\t|*|@|&]/', $anyVariable);
                susen

                Comment

                • tolkienarda
                  Contributor
                  • Dec 2006
                  • 316

                  #9
                  hmm i realy need to learn that preg_match stuff it is way simpler than my loops and such.

                  eric

                  Comment

                  • exoskeleton
                    New Member
                    • Sep 2006
                    • 104

                    #10
                    thank you sir susen and sir eric...it really helps.....more power guys

                    Comment

                    • NIKHILUNNIKRISHNAN
                      New Member
                      • Jun 2008
                      • 22

                      #11
                      I Didnt Get It Done Using "strpos". In Which Namespace Is That Function Defined ?

                      Comment

                      • NIKHILUNNIKRISHNAN
                        New Member
                        • Jun 2008
                        • 22

                        #12
                        Can I Have Any Alternate Methods For The Same ?

                        Comment

                        • RodanMedia
                          New Member
                          • Sep 2009
                          • 4

                          #13
                          This was valuable to me, as we're designing a new social network site. But here's my modification of that script, checking for a specified set of values in an array, then checking for an empty position.

                          This is the code exactly as it is in action. To see it working, go to <link removed>. I dare you to get by it.

                          Code:
                          $forbid = array(' ','--', '-', '&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
                              $i = 0;
                              while ($i < 37) {
                                  $var = strpos($userID, $forbid[$i]);
                                  if (!empty($var)) {
                                      $var++;
                                      echo "Position " . $var . " forbidden character.<br><font color='#FF0000'>Character detected is <strong>'" . $forbid[$i] . "'</strong></font><br>";
                                      echo "<p align='left'><h2><font color='red'>Oops! Only Letters and Numbers are allowed on the User ID portion.</font></h2><br><br>Please <a href='javascript:onClick=history.go(-1)' class='divProfile'>&raquo; GO BACK &laquo;</a> and remove any spaces and special characters.</p>";
                                      exit();
                                  }
                                 
                                  $i++;
                          }
                          Last edited by Markus; Sep 17 '09, 09:29 PM. Reason: Added [code] tags and removed out-bound link.

                          Comment

                          • TheServant
                            Recognized Expert Top Contributor
                            • Feb 2008
                            • 1168

                            #14
                            Thanks for your input, and welcome to Bytes. I think that rather than forbidding every character possible (black-list) it would be better to have a white-list of allowed characters. Also, what about people with hyphenated names?

                            Comment

                            • hsriat
                              Recognized Expert Top Contributor
                              • Jan 2008
                              • 1653

                              #15
                              Originally posted by exoskeleton
                              please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid!

                              please help...TIA
                              If you are talking about HTML special characters, then just do...
                              Code:
                              if (htmlspecialchars($string) != $string) {
                              //it contains special chars.
                              }

                              Comment

                              Working...