Some PHP help please

by 8 replies
9
Hi

I'm trying to code a search module that has one input text box, which then searches a database. In the text box the user is asked to enter a town or a uk postcode.

The database does not contain full postcodes, it contains post codes like BD23 1 (the postcode area). My search works fine if I enter a town, or a postcode as it is in the database, but not if I enter a full postcode (BD23 1AA). I need to strip off the AA (last two characters if they are non numeric.

I have this code:

Code:
/*IF THERES A DIGIT IN KEYWORD ASSUME POSTCODE*/
if (preg_match('/[^0-9A-Za-z]+$/',$keyword)) {

	//SEE IF LAST 2 CHARS ARE LETTERS
	$last3chars = substr($keyword, -2);
	if (!is_int(substr($last3chars, -2))) {
	$keyword = substr($keyword,0,-2);
	}
}
It does not strip off the last two characters. Would really appreciate if a php guru could help with this, as my PHP isn't that great.

Thanks in advance.
#programming #php
  • Instead of trying to see if the last characters are not numbers you can check the stings leangth

    if (strlen($keyword)==7){
    $keyword=substr($keyword,-2);
    }
    • [1] reply
    • Hi,

      Thanks for the reply.

      I can't test for the length because postcodes can be 6 - 9 characters (if a space is included between the first part and the second part), and as I won't know which postcode is entered, so I 'm not testing for that.

      Cheers
  • $last3chars = substr_replace($keyword,"", -2);

    you can use the above format.

    If you dont know how to do that let me know i will put the complete code here for that
    • [1] reply
    • Hi festi,

      I tried this and it hasn't worked, could you post the full code just in case I put it in the wrong place.

      Many thanks for your help.
      • [1] reply

Next Topics on Trending Feed

  • 9

    Hi I'm trying to code a search module that has one input text box, which then searches a database. In the text box the user is asked to enter a town or a uk postcode.