String handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Itzatez
    New Member
    • Dec 2007
    • 5

    String handling

    Hello there, first of all i would like to congratulate you guys for the great job you've been doing.. this site has helped me a lot and lots of times... :)

    I'm very newbie to PHP, only started to use this language few days ago, and i have a small answer that i cant seem to find a answer to fullfill my needs...

    I have a variable that is a string, which is a group of names divided by ',' and i would like to know if there is a way to extract each name of the string to an array of names or whatever. That variable is given by a query to the database and its like:
    name1, name2, name3...... oh and the names between the ',' can have more then one word... :S


    Thanks in advance for the help, cya later...
  • zlash
    New Member
    • Dec 2007
    • 9

    #2
    try this:

    http://us.php.net/manual/en/function.explod e.php

    explode will split a string into an array, using any character that you pass as a delimiter.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Hey, itzatez

      have a look at this thread.

      :)

      post back if you need more assistance.

      Comment

      • Itzatez
        New Member
        • Dec 2007
        • 5

        #4
        Thanks a lot guys... it worked like a charm!

        Again, thanks! :) Good weekend and happy new year...

        Comment

        • t3chn0n3rd
          New Member
          • Dec 2007
          • 19

          #5
          great your problem is resolved

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Itzatez
            Thanks a lot guys... it worked like a charm!

            Again, thanks! :) Good weekend and happy new year...
            Post back whenever you need some help

            Happy new year!

            Comment

            • Itzatez
              New Member
              • Dec 2007
              • 5

              #7
              Ok.. im back again with a new problem...

              After using what was suggested before i have now one array with names, and i need to count how many times each name repeats itself and then using that information.

              Lets say the first array is (a,b,c,a,d,a,b, e) then, counting the unique elements with array_count_val ues() i get the associative array, which would be ("[a]=>3","[b]=>2","[c]=>1","[d]=>1","[e]=>1)

              Using the extract() i can retrieve the numbers associated to each field, but my problem is... At start i dont know what is the content of the inicial array, thus i dont know what the [a], [b], [c], [d] and [e] are.. so how can i retrieve the values that correspond to those fields in the associated array? I mean, i need to use the values associated to the key's but i dont know how... Any help on this would be very appreciated since im a completly n**b at php... :)

              Thanks a lot in advance, cya later...

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                I'm sorry - i can't quite understand.

                Explain a little more?

                i'm a n!#b too :P

                Comment

                • Itzatez
                  New Member
                  • Dec 2007
                  • 5

                  #9
                  ok... so... at the begining i had this string has i spoke in the first post... i dont know the content of the string... i only know that its a bunch of names divided by ','. Using the explode() function i was able to extract into an array all the names.

                  Now i needed to count how many times the elements repeat themselfs. i used array_count_val ues() function to do it... that returned a associated array with both the names and the quantity associated to them.

                  Giving the same example that i gave in the post above, lets say the first string was: "a,b,c,a,d,a,b, e"... Using explode() i got an array("a","b"," c","a","d","a", "b","e") where this letters represent the names in the first string. Now... using the array_count_val ues() i got another array with the total numbers of each element, ("[a]=>3","[b]=>2","[c]=>1","[d]=>1","[e]=>1).

                  Now if the names were a,b,c,d,e i could use those numbers by simply calling the extract() function and using $a, $b, $c, $d or $e.. But the problem is that i dont know what those names are.

                  So my doubt is...how do i use those values given in the last array if i dont know what the names are... in other words, i have a array with the names and other array with the names, which are variables, and the count of themselfs... but how do i use this to get the value of the count.


                  Lets say the first array is ("Itzatez","Itz atez"), and the array after array_count_val ues() is ("[Itzatez]=>2")... how do i retreave that "2" if the name can be anything, but in this case is Itzatez...

                  ufff.... this is hard to explain... :S sorry for this little mess, but english is not my native language, which makes it a bit harder... :D


                  thanks for the help and pacience mate, cya.

                  Comment

                  • zlash
                    New Member
                    • Dec 2007
                    • 9

                    #10
                    Hello, I think that I understand your problem and here is a way to solve it:

                    The foreach construction in PHP will travel all the keys and values on it, if you have the array names_count[] you can use:

                    [PHP]foreach ($names_count as $key => $value)
                    {
                    //Code written here will execute once per element on the array
                    // each time, $key will hold the value of the key and $value the value of...
                    // the value :P
                    echo($key . "is repeated" . $value . "times");
                    }[/PHP]

                    Hope that it makes sense.

                    Comment

                    • Itzatez
                      New Member
                      • Dec 2007
                      • 5

                      #11
                      uhuh! that worked! :) thanks a lot mates... Im starting to love this thing on PHP that give's us a function to whatever we need... "oh... i need that" *zuuup* "oh.. theres a function to do that!"... :D


                      again, thanks for the help. Happy new year to all.

                      Comment

                      • taroo2ah
                        New Member
                        • May 2007
                        • 5

                        #12
                        Originally posted by Itzatez
                        Hello there, first of all i would like to congratulate you guys for the great job you've been doing.. this site has helped me a lot and lots of times... :)

                        I'm very newbie to PHP, only started to use this language few days ago, and i have a small answer that i cant seem to find a answer to fullfill my needs...

                        I have a variable that is a string, which is a group of names divided by ',' and i would like to know if there is a way to extract each name of the string to an array of names or whatever. That variable is given by a query to the database and its like:
                        name1, name2, name3...... oh and the names between the ',' can have more then one word... :S


                        Thanks in advance for the help, cya later...

                        Hi,
                        You can use the explode() function giving it the comma as the first parameter, and the string as the second and it will fill an array for you. It should look like the following:

                        $ArrayOfNames = explode(',' , $StringOfCommaS eparatedNames);

                        In the line above the variable $ArrayOfNames should now hold all the names contained in the variable $StringOfCommaS eparatedNames. You can then display the names as follow:

                        foreach($ArrayO fNames as $index => $val)
                        {
                        echo "$val<br>";
                        }

                        I hope this will help.

                        Comment

                        Working...