Putnammj Posted June 9, 2014 Share Posted June 9, 2014 Hi Guys, I have a search box on the site I am creating and when a user types their postcode in it the database is searched and members covering that location are shown. I have the following which works fine ... but: --------------------------------------------------------------------------------------------------------------- $postcode = mysqli_real_escape_string($myConnect, $_POST['searchbox']); #Convert the post code to upper case and trim the variable $postcode = strtoupper(trim($postcode)); #Remove any spaces $postcode = str_replace(" ","",$postcode); #Trim the last 3 characters off the end $postcode_wc = substr($postcode,0,strlen($postcode)-3); ------------------------------------------------------------------------------------- The problem i have is that if the user just types in the first part of the poscode such as SW1 it is then trimmed to zero characters or SW10 is trimmed to just S and this wont return the results! I need the last part of the code to only run if they enter 5 character or more! What should i add before or add to the last part of the code (shown in bold) Please Help! Thank you in advance kind souls!!!! Mark Quote Link to comment https://forums.phpfreaks.com/topic/289085-trimming-help-php-please-take-a-look/ Share on other sites More sharing options...
Solution maxxd Posted June 9, 2014 Solution Share Posted June 9, 2014 if(strlen($postcode) > 5){ $postcode_wc = substr($postcode,0,-3); } Give that a shot. Quote Link to comment https://forums.phpfreaks.com/topic/289085-trimming-help-php-please-take-a-look/#findComment-1482311 Share on other sites More sharing options...
Putnammj Posted June 9, 2014 Author Share Posted June 9, 2014 Thanks so much worked a treat! Quote Link to comment https://forums.phpfreaks.com/topic/289085-trimming-help-php-please-take-a-look/#findComment-1482321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.