473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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...
Dec 28 '07 #1
11 1409
zlash
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
6,050 Expert 4TB
Hey, itzatez

have a look at this thread.

:)

post back if you need more assistance.
Dec 28 '07 #3
Thanks a lot guys... it worked like a charm!

Again, thanks! :) Good weekend and happy new year...
Dec 29 '07 #4
great your problem is resolved
Dec 29 '07 #5
Markus
6,050 Expert 4TB
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
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
6,050 Expert 4TB
I'm sorry - i can't quite understand.

Explain a little more?

i'm a n!#b too :P
Dec 29 '07 #8
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
zlash
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
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
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

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

Similar topics

6
by: lkrubner | last post by:
Last year I asked a bunch of questions about character encoding on this newsgroup. All the answers came down to using ord() in creative ways to try to make guesses about multi-byte characters. I...
4
by: Ken Fine | last post by:
No joy on Macromedia's boards after a few days; maybe someone can help me here. I got an excellent string handling function off of planet-source-code.com that converts text strings to proper...
4
by: Rajeev | last post by:
Hi I am a VB programmer. I need to do a simple thing in VC++. char strNWUserName; DWORD dBufferLength = 256; HRESULT hr; hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength); The...
1
by: Derek | last post by:
A common technique for trimming leading and trailing spaces from std::string is the following: string s(" blah blah blah "); const char* ws= " \t\r"; string::size_type not_white; // trim...
6
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
12
by: kevineller794 | last post by:
I want to make a split string function, but it's getting complicated. What I want to do is make a function with a String, BeginStr and an EndStr variable, and I want it to return it in a char...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.