Web Analytics Made Easy -
StatCounter checking string for ' - CodingForum

Announcement

Collapse
No announcement yet.

checking string for '

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • checking string for '

    I'm using php.
    I have a text box in a form and I want to check if they have typed in a ' charactor in that field. How do i do that in php.
    Thanks

  • #2
    PHP Code:
    if (strstr ($string_to_search"'")!=false){
      
    //Yup, they typed it.
    }else{
      
    //Nope, they didn't type it.

    Note that if you're trying to comment them out, you can use
    PHP Code:
    addslashes() 
    or to remove the slashes, you can use
    PHP Code:
    stripslashes() 
    Offtone.com - In the works...

    Comment


    • #3
      aaron was nearly right... tho one error.. use this code

      PHP Code:
      if (strstr ($string_to_search"'")!==false){
        
      //Yup, they typed it.
      }else{
        
      //Nope, they didn't type it.

      Jee

      P.s. junior members eh
      Jeewhizz - MySQL Moderator
      http://www.sitehq.co.uk
      PHP and MySQL Hosting

      Comment


      • #4
        Hey, what was my mistake? the !== instead of !=?

        Well check THIS out, mista:

        http://www.aaron-wright.com/grr.php



        If I'm still wrong, please clarify. lol
        Offtone.com - In the works...

        Comment


        • #5
          i'd have to say that aarons right !== is not correct AFAIK, unless PHP has been changed
          "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

          (Me Me Me Me Me Me Me Me Me)

          Comment


          • #6
            Jee may be right actually, but only if you're using the latest version of PHP I heard there were some changes being made and that we'll all have to get used to them, but then again I could be terribly misinformed and maybe there isn't even a new version of PHP

            I need to sit down.
            Offtone.com - In the works...

            Comment


            • #7
              or just
              PHP Code:
              if(strstr($string_to_search"'")){

                
              //Yup, they typed it.

              }else{

                
              //Nope, they didn't type it.


              because not false is the same as true...
              Projects I work on: http://www.devnet.info, http://www.mybboard.com
              Forums I visit: http://www.nemesis.thecoldshop.com, http://www.the-pit.net, http://www.hardforum.com, http://www.genmay.com

              Comment


              • #8
                ...but being exact, the return value of strstr() isn't a boolean true either - it's only casted to true in the if-statement.

                Apparently Jeewhizz confused strstr() with strpos(), because with strpos() you have to be wary of a returned 0 which gets casted to false, that's why you should use !== instead of !=.

                http://www.php.net/manual/en/function.strpos.php
                De gustibus non est disputandum.

                Comment


                • #9
                  *reads post and laughs to self*

                  Comment

                  Working...
                  X