•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,468 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,813 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 649 | Replies: 3
![]() |
•
•
Join Date: Jul 2004
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
hello all,
Im trying to dynamically insert text onto a page by inserting a value from a query string using php. This was simple enough when i wanted to insert the entire value, but im having trouble finding information on how to search the value for a word (from a list of words) and if it contains one to echo/print just that word, not the entire value.
So far i have the basic 'return the whole value' scripts.
[code:php]
<?php if($_GET['KEY'] == "")
{
// no keyword entered
echo "";
}
else
{
echo $_GET['KEY']." Or Any City";
}
?>
</span>
[/code]
The value of the key contains a location eg. washington, the value could just be one word(and the above code works if it is), but its more likely to contain more words eg. ?key=washington something somethingelse
I need to find a way of searching this value against a list of locations (eg. washington, new york, la, etc.. If the value of the key contains one of the locations in the list, then i need to insert just that location into the page, not the entire value.
Im not sure how this is best done, if anyone has any ideas or knows where i can find help, it'd be much appreciated.
Thanks, Rika
Im trying to dynamically insert text onto a page by inserting a value from a query string using php. This was simple enough when i wanted to insert the entire value, but im having trouble finding information on how to search the value for a word (from a list of words) and if it contains one to echo/print just that word, not the entire value.
So far i have the basic 'return the whole value' scripts.
[code:php]
<?php if($_GET['KEY'] == "")
{
// no keyword entered
echo "";
}
else
{
echo $_GET['KEY']." Or Any City";
}
?>
</span>
[/code]
The value of the key contains a location eg. washington, the value could just be one word(and the above code works if it is), but its more likely to contain more words eg. ?key=washington something somethingelse
I need to find a way of searching this value against a list of locations (eg. washington, new york, la, etc.. If the value of the key contains one of the locations in the list, then i need to insert just that location into the page, not the entire value.
Im not sure how this is best done, if anyone has any ideas or knows where i can find help, it'd be much appreciated.
Thanks, Rika
Maybe something?
You might also want to look at the function
Btw, the above is untested and straight from my head, it may or may not work
<?php
$word_to_match = "New York";
$locations = array("Washington","New York","Brooklyn");
foreach ($locations as $c)
{
if ( $c == $word_to_match)
{
echo "Word found : $c <br>";
}
else
echo "Nope <br>";
}
?>You might also want to look at the function
array_push which is presumably more flexible.Btw, the above is untested and straight from my head, it may or may not work
If the user input is ,for example "house Seattle job" and you wanna get the Seattle word, you can do this with explode http://www.php.net/manual/en/function.explode.php
Now you must loop through the userCity array and compare with another array or with what you used to store the city names
Here you must have a convetion.All the citynames must be in lower/uppercase in order to get a good comparison . So you must do an additional operation before using the for : $userInputCity=strtolower($_GET['key']) assuming that the names in cities array are also lowercase
If the input city name is composed of a single word , this would work fine , but if is something like New York , Los Angeles than in the for loop you must add :
Later edit :
After a second thought , it's much easier to just use the strpos() , like this :
php if($_GET['KEY'] == "")
{
// no keyword entered
echo "";
}
else
{
$userCity=array();
$userCity=explode(" ",$_GET['key']);Here you must have a convetion.All the citynames must be in lower/uppercase in order to get a good comparison . So you must do an additional operation before using the for : $userInputCity=strtolower($_GET['key']) assuming that the names in cities array are also lowercase
foreach($userCity as $key1 => $inputcity)
{
foreach($cities as $key2 => $cityname)
{
if($inputcity == $cityname)
echo 'found a city';
}
}If the input city name is composed of a single word , this would work fine , but if is something like New York , Los Angeles than in the for loop you must add :
elseif($key1 != 0 )
{
$twoWordCity=$userCity[$key1-1]." ".$userCity[$key1];
if( $twoWordCity == $cityname )
echo 'found a city';
}Later edit :
After a second thought , it's much easier to just use the strpos() , like this :
$myCityNamesArray=('london','new york','sydney',......);
$cityInput=strtolower($_GET['key']);
foreach($myCityNamesArray as $cityName )
{
if( strpos( $cityInput , $cityName ) !== false )
{
echo 'found it ';
break;
}
} Last edited by Eko : Sep 16th, 2007 at 8:56 pm.
"Get rich or die tryin"(50-cent)
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- help a newbie? mySQL find & replace (MySQL)
- Programming FAQ - Updated 1/March/2005 (Computer Science and Software Design)
- how to find.................. (Windows NT / 2000 / XP / 2003)
- insert data from one form into two diffent database table (JSP)
- Problem with new drive? (Windows NT / 2000 / XP / 2003)
- Storing dynamic form values in Arrays for display & insert (PHP)
Other Threads in the PHP Forum
- Previous Thread: quickly getting from sql
- Next Thread: HTML In Mail



Linear Mode