php strings

Associate
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
hey guys bit of a pickle here.

i have example text that i get from a text box from a user and looking at the best way to split them up using the least amount of code possible.

i have a php class that stores

number of rooms [1,2....10]
type [rent/let or sale]
area [the area it's in]
price [the price]


here are some example searches.

* 5 bed rent
* Camden 3 bedroom for sale
* 2 bed apartment to let London
* 3 or 4 bed house to rent in Notting Hill for 1000 per month

what's the best way to extract the data using php with out me knowing what way the user will enter the data.

this is for a progamming test so it doesn't have to be 100% functional.

thanks.
 
Associate
Joined
11 Jun 2009
Posts
813
As this is for a test, are they trying to find out if your going to use a regex (or multiples) to possibly extract the data?

I guess your going to have to remove/ignore words such as bed/bedroom/to/for.. so that anything left in the string will be either the "area" or the "type".
You obvioulsy can put the type in to an array to extract them thus leaving everything else to be area.

Any numeric < 11 I would assume as number of rooms looking at your php class loose example, but would add a condition in the regex to look possibly for xk just incase somebody would ask for 5k as a price for example.
This then would leave your price (unless it met the previous condition)
 
Last edited:
Associate
OP
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
well it's for a test for a job.

basically they specify that the key data that i want could be in any format and just give them four examples.

they say that things like number of bedrooms will always be an int and not string ie. 4 instead of "four" same for the money values.

just really hit a brick wall on where to start.

like i've done regexes in perl before but i find them harder in php especially extracting the matches into variables it's different in php
 
Last edited:
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
If it's any help I found this tool very helpful when plying around with regular expressions http://www.gskinner.com/RegExr/

They are compatible with PHP as long as you enclose it in a pair of forward slashes. For example:

$regex = '/regex string here/';
 
Soldato
Joined
15 Nov 2008
Posts
5,060
Location
In the ether
hey guys bit of a pickle here.

i have example text that i get from a text box from a user and looking at the best way to split them up using the least amount of code possible.

i have a php class that stores

number of rooms [1,2....10]
type [rent/let or sale]
area [the area it's in]
price [the price]


here are some example searches.

* 5 bed rent
* Camden 3 bedroom for sale
* 2 bed apartment to let London
* 3 or 4 bed house to rent in Notting Hill for 1000 per month

what's the best way to extract the data using php with out me knowing what way the user will enter the data.

this is for a progamming test so it doesn't have to be 100% functional.

thanks.

Hmm that's going to be very tricky IMO.

To fudge it, I'd probably have a set of key terms (let rent sale buy) and a location lookup tool so given a string see if it's a location,

I'd split the string into an array of strings
@words = split / /, $input_string;
my NewSearch $search = new Search();

foreach $entry (@words){
if(checkLocation($entry)!=0){
$search->$location = checkLocation($entry)
next;
}
if(checkPurchaserType($entry) !=0){
$search->$purchase_type = checkPurchaserType($entry);
}

}

class Search{
$location;
$purchase_type;
$nbeds;
$rent_period;
}


Bit of a bodge of all sorts of coding (mainly perl) but I think you can get the picture
 
Associate
OP
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
ok i've got this working, but i've snagged another problem.

i get the results i want for a SOAP request.

PHP:
$response = $API->search_rental($parameters);
$results = $response->results;
      					
print "$results->search_sentence <br />";
			
			
foreach($results->ads as $ad)
{
	if($ad->area == $this->get_area())
	{
		printf('<a href="%s">%s</a><br />', $ad->url, $ad->full_address);
    	}
    	else
    	{
    		printf('<a href="%s">%s</a><br />', $ad->url, $ad->full_address);
    	}
 }

what i want to do is if $this->get_area() returns a value i want to only print out that ads that contain the same value in $this->get_area() as in $ad->area, but if $this->get_area() doesn't hold a value i want to print out all the ads.

the code above will print out all the ads regardless.

anyone any pointers?
 
Top Bottom