944,179 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1087
  • PHP RSS
Sep 15th, 2007
0

Find & Insert Part of urlQuery value?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rika is offline Offline
4 posts
since Jul 2004
Sep 15th, 2007
0

Re: Find & Insert Part of urlQuery value?

Put the key locations in an array and loop through the array?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 15th, 2007
0

Re: Find & Insert Part of urlQuery value?

Maybe something?

PHP Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 16th, 2007
0

Re: Find & Insert Part of urlQuery value?

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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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 :

PHP Syntax (Toggle Plain Text)
  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 :
PHP Syntax (Toggle Plain Text)
  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.
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: quickly getting from sql
Next Thread in PHP Forum Timeline: HTML In Mail





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC