strpos - and multiple needles?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jodleren

    #1

    strpos - and multiple needles?

    Hi!

    I was reading php.net for a way to find a number of characters in a
    strings. Say I want to look for a b and c ind $string.
    As far as I could see, there is no function for that.

    So, I can either?
    1) run through the string and test using in_array('a', 'b', 'c')
    2) run through array and test using strpos( $items[$i]....
    Where I find #2 the best.

    But is there a function which can do that faster or in a better way?
  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: strpos - and multiple needles?

    jodleren wrote:
    Hi!
    >
    I was reading php.net for a way to find a number of characters in a
    strings. Say I want to look for a b and c ind $string.
    As far as I could see, there is no function for that.
    preg_match("[abc]",$subject) ;


    You might find reading something on regular expressions useful.


    Cheers,
    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Proudly running Debian Linux with 2.6.22-3-amd64 kernel, KDE 3.5.8, and PHP
    5.2.4-2 generating this signature.
    Uptime: 14:37:42 up 36 days, 53 min, 4 users, load average: 0.40, 0.81,
    0.94

    Comment

    • My Pet Programmer

      #3
      Re: strpos - and multiple needles?

      jodleren said:
      Hi!
      >
      I was reading php.net for a way to find a number of characters in a
      strings. Say I want to look for a b and c ind $string.
      As far as I could see, there is no function for that.
      >
      So, I can either?
      1) run through the string and test using in_array('a', 'b', 'c')
      2) run through array and test using strpos( $items[$i]....
      Where I find #2 the best.
      >
      But is there a function which can do that faster or in a better way?
      So you have one string, and you want to see if each of your elements are
      in it individually.

      So:

      $needles = array("a", "b", "c");
      $haystack = "This is a string with a certain number of each character in
      it.";

      $counts = array();
      function countChars($let ter, $key, $arr) {
      global $output;
      $garbage = explode($letter , $arr[0]);
      $arr[1][$letter] = count($garbage)-1;

      }
      array_walk($nee dles, 'countChars', array($haystack , &$counts));


      print_r($counts ); // Array ( [a] =6 [b] =1 [c] =4 )

      That's a little cleaner, I suppose. You're still going through a bunch,
      but if you're just counting, that will return an array that looks like that.

      All the best,
      ~A!

      --
      Anthony Levensalor
      anthony@mypetpr ogrammer.com

      Only two things are infinite, the universe and human stupidity,
      and I'm not sure about the former. - Albert Einstein

      Comment

      • My Pet Programmer

        #4
        Re: strpos - and multiple needles?

        Iván Sánchez Ortega said:
        jodleren wrote:
        [snip]
        preg_match("[abc]",$subject) ;
        Oh, see, that's much better than what I did.

        You might find reading something on regular expressions useful.
        And so, it seems, would I. Thanks!

        ~A!
        --
        Anthony Levensalor
        anthony@mypetpr ogrammer.com

        Only two things are infinite, the universe and human stupidity,
        and I'm not sure about the former. - Albert Einstein

        Comment

        • My Pet Programmer

          #5
          Re: strpos - and multiple needles?

          Iván Sánchez Ortega said:
          jodleren wrote:
          >Hi!
          >>
          >I was reading php.net for a way to find a number of characters in a
          >strings. Say I want to look for a b and c ind $string.
          >As far as I could see, there is no function for that.
          preg_match("[abc]",$subject) ;


          You might find reading something on regular expressions useful.


          Cheers,
          That will only tell you if any of the characters are in the string. Do
          you have one that will say whether all of them are in the haystack?





          --
          Anthony Levensalor
          anthony@mypetpr ogrammer.com

          Only two things are infinite, the universe and human stupidity,
          and I'm not sure about the former. - Albert Einstein

          Comment

          • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

            #6
            Re: strpos - and multiple needles?

            My Pet Programmer wrote:
            >>I was reading php.net for a way to find a number of characters in a
            >>strings. Say I want to look for a b and c ind $string.
            >>As far as I could see, there is no function for that.
            >>
            >preg_match("[abc]",$subject) ;
            >
            That will only tell you if any of the characters are in the string. Do
            you have one that will say whether all of them are in the haystack?
            Are you looking for http://php.net/count_chars ??

            What are you trying to do, exactly?

            --
            ----------------------------------
            Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

            MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
            Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net

            Comment

            • My Pet Programmer

              #7
              Re: strpos - and multiple needles?

              Iván Sánchez Ortega said:
              My Pet Programmer wrote:
              >>>I was reading php.net for a way to find a number of characters in a
              >>>strings. Say I want to look for a b and c ind $string.
              >>>As far as I could see, there is no function for that.
              >>preg_match( "[abc]",$subject) ;
              >That will only tell you if any of the characters are in the string. Do
              >you have one that will say whether all of them are in the haystack?
              Are you looking for http://php.net/count_chars ??

              What are you trying to do, exactly?
              Oh, for me it was just idle curiosity. I'm not sure what the OP is after.

              ~A!

              --
              Anthony Levensalor
              anthony@mypetpr ogrammer.com

              Only two things are infinite, the universe and human stupidity,
              and I'm not sure about the former. - Albert Einstein

              Comment

              • jodleren

                #8
                Re: strpos - and multiple needles?

                On Dec 28, 4:43 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                escomposlinux.-.punto.-.orgwrote:
                My Pet Programmer wrote:
                >I was reading php.net for a way to find a number of characters in a
                >strings. Say I want to look for a b and c ind $string.
                >As far as I could see, there is no function for that.
                preg_match("[abc]",$subject) ;
                That will only tell you if any of the characters are in the string. Do
                you have one that will say whether all of them are in the haystack?
                Are you looking forhttp://php.net/count_chars??
                What are you trying to do, exactly?
                To check for invalid characters in a filename

                WBR
                Sonnich

                Comment

                • My Pet Programmer

                  #9
                  Re: strpos - and multiple needles?

                  jodleren said:
                  On Dec 28, 4:43 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                  escomposlinux.-.punto.-.orgwrote:
                  >My Pet Programmer wrote:
                  >>>>I was reading php.net for a way to find a number of characters in a
                  >>>>strings. Say I want to look for a b and c ind $string.
                  >>>>As far as I could see, there is no function for that.
                  >>>preg_match ("[abc]",$subject) ;
                  >>That will only tell you if any of the characters are in the string. Do
                  >>you have one that will say whether all of them are in the haystack?
                  >Are you looking forhttp://php.net/count_chars??
                  >What are you trying to do, exactly?
                  To check for invalid characters in a filename

                  WBR
                  Sonnich
                  preg_match("*[abc]*", $haystack); worked for me.

                  ~A!



                  --
                  Anthony Levensalor
                  anthony@mypetpr ogrammer.com

                  Only two things are infinite, the universe and human stupidity,
                  and I'm not sure about the former. - Albert Einstein

                  Comment

                  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

                    #10
                    Re: strpos - and multiple needles?

                    jodleren wrote:
                    To check for invalid characters in a filename
                    Escape the filename, then.

                    --
                    ----------------------------------
                    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

                    MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
                    Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net

                    Comment

                    • jodleren

                      #11
                      Re: strpos - and multiple needles?

                      On Dec 28, 5:20 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                      escomposlinux.-.punto.-.orgwrote:
                      jodleren wrote:
                      To check for invalid characters in a filename
                      Escape the filename, then.
                      Sorry, I dont get this one.

                      Comment

                      • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

                        #12
                        Re: strpos - and multiple needles?

                        jodleren wrote:
                        On Dec 28, 5:20 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                        escomposlinux.-.punto.-.orgwrote:
                        >jodleren wrote:
                        To check for invalid characters in a filename
                        >Escape the filename, then.
                        >
                        Sorry, I dont get this one.
                        OK... why do you need to check for invalid characters in a filename?

                        In other words, where could a filename with "strange" characters cause
                        damage to your system?

                        --
                        ----------------------------------
                        Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

                        La costumbre es una segunda naturaleza.- Galeno.

                        Comment

                        • jodleren

                          #13
                          Re: strpos - and multiple needles?

                          On Dec 28, 5:41 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                          escomposlinux.-.punto.-.orgwrote:
                          jodleren wrote:
                          On Dec 28, 5:20 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                          escomposlinux.-.punto.-.orgwrote:
                          jodleren wrote:
                          To check for invalid characters in a filename
                          Escape the filename, then.
                          >
                          Sorry, I dont get this one.
                          >
                          OK... why do you need to check for invalid characters in a filename?
                          >
                          In other words, where could a filename with "strange" characters cause
                          damage to your system?
                          Windows based systems do not allow certain characters. I need to check
                          for them, _even_ that my server can handle them, I still have to think
                          of the user downloading a file etc....

                          Comment

                          • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

                            #14
                            Re: strpos - and multiple needles?

                            jodleren wrote:
                            Windows based systems do not allow certain characters. I need to check
                            for them, _even_ that my server can handle them, I still have to think
                            of the user downloading a file etc....
                            Then, you do not want to check for the positions of the invalid characters
                            (as you said on your original post).

                            You want to run a preg_match (to detect) or preg_replace (to fix) on the
                            filename string.

                            --
                            ----------------------------------
                            Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

                            Un ordenador no es un televisor ni un microondas, es una herramienta
                            compleja.

                            Comment

                            • My Pet Programmer

                              #15
                              Re: strpos - and multiple needles?

                              jodleren said:
                              On Dec 28, 5:41 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                              escomposlinux.-.punto.-.orgwrote:
                              >jodleren wrote:
                              >>On Dec 28, 5:20 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
                              >>escomposlinux .-.punto.-.orgwrote:
                              >>>jodleren wrote:
                              >>>>To check for invalid characters in a filename
                              >>>Escape the filename, then.
                              >>Sorry, I dont get this one.
                              >OK... why do you need to check for invalid characters in a filename?
                              >>
                              >In other words, where could a filename with "strange" characters cause
                              >damage to your system?
                              Windows based systems do not allow certain characters. I need to check
                              for them, _even_ that my server can handle them, I still have to think
                              of the user downloading a file etc....
                              Been there, definitely.


                              preg_match('/^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![
                              ]))*(\.[a-zA-Z]{2,6})$/', $filename);

                              That one should only match valid filenames.

                              ~A!




                              --
                              Anthony Levensalor
                              anthony@mypetpr ogrammer.com

                              Only two things are infinite, the universe and human stupidity,
                              and I'm not sure about the former. - Albert Einstein

                              Comment

                              Working...