By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

String handling

P: 5
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...
Dec 28 '07 #1
Share this Question
Share on Google+
11 Replies


P: 9
try this:

http://us.php.net/manual/en/function.explode.php

explode will split a string into an array, using any character that you pass as a delimiter.
Dec 28 '07 #2

Markus
Expert 5K+
P: 6,050
Hey, itzatez

have a look at this thread.

:)

post back if you need more assistance.
Dec 28 '07 #3

P: 5
Thanks a lot guys... it worked like a charm!

Again, thanks! :) Good weekend and happy new year...
Dec 29 '07 #4

P: 19
great your problem is resolved
Dec 29 '07 #5

Markus
Expert 5K+
P: 6,050
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!
Dec 29 '07 #6

P: 5
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_values() 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...
Dec 29 '07 #7

Markus
Expert 5K+
P: 6,050
I'm sorry - i can't quite understand.

Explain a little more?

i'm a n!#b too :P
Dec 29 '07 #8

P: 5
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_values() 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_values() 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","Itzatez"), and the array after array_count_values() 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.
Dec 29 '07 #9

P: 9
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.
Dec 29 '07 #10

P: 5
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.
Dec 30 '07 #11

taroo2ah
P: 5
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(',' , $StringOfCommaSeparatedNames);

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

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

I hope this will help.
Dec 30 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.