Find & Insert Part of urlQuery value?

Reply

Join Date: Jul 2004
Posts: 4
Reputation: Rika is an unknown quantity at this point 
Solved Threads: 0
Rika Rika is offline Offline
Newbie Poster

Find & Insert Part of urlQuery value?

 
0
  #1
Sep 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Find & Insert Part of urlQuery value?

 
0
  #2
Sep 15th, 2007
Put the key locations in an array and loop through the array?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Find & Insert Part of urlQuery value?

 
0
  #3
Sep 15th, 2007
Maybe something?

  1. <?php
  2.  
  3. $word_to_match = "New York";
  4.  
  5. $locations = array("Washington","New York","Brooklyn");
  6.  
  7. foreach ($locations as $c)
  8. {
  9. if ( $c == $word_to_match)
  10. {
  11. echo "Word found : $c <br>";
  12. }
  13. else
  14. echo "Nope <br>";
  15. }
  16. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: Find & Insert Part of urlQuery value?

 
0
  #4
Sep 16th, 2007
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
  1. php if($_GET['KEY'] == "")
  2. {
  3. // no keyword entered
  4. echo "";
  5. }
  6. else
  7. {
  8. $userCity=array();
  9. $userCity=explode(" ",$_GET['key']);
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
  1. foreach($userCity as $key1 => $inputcity)
  2. {
  3. foreach($cities as $key2 => $cityname)
  4. {
  5. if($inputcity == $cityname)
  6. echo 'found a city';
  7. }
  8. }

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 :

  1. elseif($key1 != 0 )
  2. {
  3. $twoWordCity=$userCity[$key1-1]." ".$userCity[$key1];
  4. if( $twoWordCity == $cityname )
  5. echo 'found a city';
  6. }


Later edit :

After a second thought , it's much easier to just use the strpos() , like this :
  1. $myCityNamesArray=('london','new york','sydney',......);
  2.  
  3. $cityInput=strtolower($_GET['key']);
  4.  
  5. foreach($myCityNamesArray as $cityName )
  6. {
  7. if( strpos( $cityInput , $cityName ) !== false )
  8. {
  9. echo 'found it ';
  10. break;
  11. }
  12. }
Last edited by Eko; Sep 16th, 2007 at 8:56 pm.
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC