473,800 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String handling

5 New Member
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 1427
zlash
9 New Member
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.
Dec 28 '07 #2
Markus
6,050 Recognized Expert Expert
Hey, itzatez

have a look at this thread.

:)

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

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

Explain a little more?

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

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

Similar topics

6
1914
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 was a little amazed at this and wondered if I'd somehow misunderstood the situation. I'm pleased to find that Joel Spolsky shared my amazement and offered some criticism of PHP on these grounds: "When I discovered that the popular web development...
4
1895
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 case, i.e. "the REMAINS of the DAY" is converted to "The Remains Of The Day". This function is particularly good in that it handles certain exceptions. Can you help me make it handle more? I'm interested in two tweaks, one easy, one hard.
4
2052
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 strNWUserName returns .CN=UserName.O=DOMAIN I just need to extract the UserName from this using VC++. I don't need the .CN= and O.=DOMAIN.
1
4051
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 leading whitespace not_white = s.find_first_not_of(ws);
6
1855
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 of the ways of doing this are 1. Read each line and do string compare immediately. 2. Read each line, add each string into a stl vector and search for the given string using stl bineary_search algorithm.
17
4674
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
32
14906
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
4
3495
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 substrings need be replaced, or one character needs be changed, what shall I do? Is it better to convert strings to UCS-32 before manipulation? But on Windows, wchar_t is 16 bits which isn't enough for characters which can't be simply encoded...
11
10866
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 a better way of doing this? if ("control" == commandString) { } else if ("execute" == commandString)
12
1927
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 array. For example: char teststr; strcpy(teststr, "test:blah;");
0
9695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9555
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10514
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10260
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10042
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.