473,804 Members | 3,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Turning words in string into individual variables

bugboy
160 New Member
I have a comma delineated string from an html form:

"wind, sun, ocean, lake"

i can use explode to turn it into an array but what i want to do is take each word and assign it to it's own variable (The number of words in the string may change), to end up with this:

$word1 == 'wind'
$word2 == 'sun'
$word3 == 'ocean'
$word4 == 'lake'

The reason is that I want to be able to put each variable into a single mysql query that cross references all the words at once.

Thanks!
Oct 26 '07 #1
7 2323
Lumpy
69 New Member
Could use just put all the words into an array, then just count how many values you have in the array and use each one as you need it? You know what I mean, that is almost what you are doing with $word1, $word2 and so on. Put everything into the $word array and then use count() to see how many you have and then call each one with something like $word[$c].
Oct 27 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, bugboy.

Combine explode() with extract().
Oct 27 '07 #3
bugboy
160 New Member
cool.. Thanks guys!

I've been reading up on extract() and i think i understand what is happening.. but.. what is a "local symbol table"?
Oct 27 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, bugboy.

A symbol is a variable, function or class name.

In other words, when you use a variable, function or class:
Expand|Select|Wrap|Line Numbers
  1. echo $someVar;
  2.  
PHP looks up the symbol ('$someVar') in its table of existing symbols to find the [location in memory of the] value of the variable with that name.

If the symbol hasn't been defined yet, PHP throws an error of E_NOTICE severity.

There are multiple symbol tables; each represents a scope. For example:

Expand|Select|Wrap|Line Numbers
  1. $global = 'not available to example()';
  2.  
  3. function example( $var1 , $var2 )
  4. {
  5.     $var3 = ' some value ';
  6.  
  7.     return $var1 . $var3 . $var2;
  8. }
  9.  
example()'s symbol table contains $var1, $var2 and $var3, but not $global.
Meanwhile, the global symbol table contains $global, but not $var1, $var2 nor $var3.

Note that this only applies to variables; functions and classes are always available anywhere after they've been defined.
Oct 27 '07 #5
bugboy
160 New Member
Wow, thanks pbmods!

So the 'global symbol table' is available to the whole page and a super global is available across multiple pages?

Can i add a value to the superglobal(?) symbol table that has a multi page scope without a form submit?
Oct 28 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, bugboy.

Not quite. Both superglobals and globals are available to the global scope of every script for a given page load. The difference is that superglobals can be used anywhere, whereas globals are only available in the global scope or by using the $GLOBALS superglobal (or the global keyword).

Here are a couple of examples:

$_SESSION is a superglobal:
Expand|Select|Wrap|Line Numbers
  1. echo $_SESSION['key'];    // Works
  2.  
  3. function doSomething()
  4. {
  5.     echo $_SESSION['key'];    // Also works.
  6. }
  7.  
On the other hand, $someVar is just a regular global:
Expand|Select|Wrap|Line Numbers
  1. $someVar = 5;
  2.  
  3. echo $someVar;    // Works
  4.  
  5. function dontDoAnything()
  6. {
  7.     echo $someVar;    // Doesn't work.
  8. }
  9.  
  10. function doSomething()
  11. {
  12.     global $someVar;
  13.  
  14.     echo $someVar;    // Works.
  15. }
  16.  
  17. function doSomethingElse()
  18. {
  19.     echo $GLOBALS['someVar'];    // Works; note that $GLOBALS is a superglobal.
  20. }
  21.  
  22. function orYouCouldDoThis()
  23. {
  24.     $someVar =& $GLOBALS['someVar'];    // Note that this is identical to "global $someVar;"
  25.  
  26.     echo $someVar;    // Works.
  27. }
  28.  
Oct 28 '07 #7
bugboy
160 New Member
Thanks pbmods, i really appreciate all your help and explanations. It helps immensely with not only what to do but why.

BugBoy
Oct 30 '07 #8

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

Similar topics

4
8747
by: mr_burns | last post by:
Hi there, I have a text box that I will use to search a database. I would like to use it so that it will not use a whole string (ie. 'red striped top') but instead break it up into individual words (ie. 'red', 'striped', 'top') and maybe put it into and array. I will then use the words in the array to search the database. How do I break a string up this way?
20
8713
by: Brian Kazian | last post by:
Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using pre-existing variables. They then enter numerical values for these variables, or can tell the program to randomize the values used within a certain bounds. My problem is taking in this equation they have written in the text field and converting it into...
10
2444
by: Aaron | last post by:
string a = "i have an apple"; i want the output to be "I Have An Apple"; i was able to capitalize the first word but can't figure out how to capitalize every word in that string Thanks, Aaron
5
3136
by: derrick | last post by:
I used to program text RPG's in C back in high school, but I was very crude at it and only learned what I needed to get the job done. I am now about to graduate college with a degree in English and have had an interest in the structure of language. In order to begin with my little program, I need to be able to take a string like "The dog jumped." and be able to take the first word from the sentence and assign it to a string, such as word1,...
40
10908
by: Alex | last post by:
Hello, does anybody know how to turn off the autocomplete feature for a certain text field? I am aware of the "autocomplete" attribute, but I have seen other implementions achieving it without any such attribute and am wondering whether there is another way to do it .... like via CSS or a naming convention or .......
3
2758
by: Maya | last post by:
Hello guys, Is there an easy way to extract individual words that form a string and store them in variables like in this example: String "how are you?" My result would be: var1 = "how"
4
12682
by: J | last post by:
anybody know a vb.net command to populate an array with each word in a string? for example " hi how are you" array(0) = hi array(1) = how array(2) = are array(3) = you i know i could loop though and search for spaces and build it that way...but a built in command would be great.
7
3202
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends up looking for characters that wrap around the stripped characters and it ends up as a recursive ordeal that fails to identify a poorly constructed $_GET variable (when someone hand-types the item into the line and makes a simple typing error).
3
5252
by: Ramper | last post by:
I need to Write a function that will, given an input string containing many words, split that string into individual words. For each word, the function should output the word, its starting index in the string, and its length to the console without using stream extraction operator. #include <iostream> #include <string> using namespace std; int main() { string sval = " The quick brown fox jumps over the lazy dog";
0
9706
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
9582
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
10580
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...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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
9157
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...
1
7621
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.