micky007 Posted November 11, 2011 Share Posted November 11, 2011 Hi Guys, I've managed to code what i need it do to but i have an issue, below is my code: $postcode=$_GET['postcode']; function countchar ($string) { $resultpostcode = strlen ($string) - substr_count($string, ' '); echo $resultpostcode; } countchar ($postcode); if ( $resultpostcode == 6 ) { echo "Postcode1 "; echo substr($postcode, 0, 3); echo "<br>"; echo "Postcode2 "; echo substr($postcode, 3); } elseif( $resultpostcode == 7 ){ echo "Postcode1 "; echo substr($postcode, 0, 4); echo "<br>"; echo "Postcode2 "; echo substr($postcode, 4); }else { echo "error"; } If the value of postcoderesult is 6 (it even outputs 6) then it wont echo what it should be doing, the same if its 7. It just keeps echoing Error. Any help would be great please. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/250926-php-help-asap-please/ Share on other sites More sharing options...
joe92 Posted November 11, 2011 Share Posted November 11, 2011 I'm not entirely sure what you are trying to achieve, if you could elaborate on that please. But, good practice is change your function to return the result rather than echo, then call it in a variable afterwards; $postcode=$_GET['postcode']; function countchar ($string) { $resultpostcode = strlen ($string) - substr_count($string, ' '); return $resultpostcode; } $resultpostcode = countchar ($postcode); if ( $resultpostcode == 6 ) { echo "Postcode1 "; echo substr($postcode, 0, 3); echo "<br>"; echo "Postcode2 "; echo substr($postcode, 3); } elseif( $resultpostcode == 7 ){ echo "Postcode1 "; echo substr($postcode, 0, 4); echo "<br>"; echo "Postcode2 "; echo substr($postcode, 4); }else { echo "error"; } Quote Link to comment https://forums.phpfreaks.com/topic/250926-php-help-asap-please/#findComment-1287301 Share on other sites More sharing options...
micky007 Posted November 11, 2011 Author Share Posted November 11, 2011 What im trying to do is get how many characters there are in the variable called postcode. If theres 6 characters then it will echo the code. If theres 7 characters then it will echo different code. The script also removes any spaces in the data that the variable holds. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/250926-php-help-asap-please/#findComment-1287306 Share on other sites More sharing options...
Pikachu2000 Posted November 11, 2011 Share Posted November 11, 2011 You need to read up on variable scope in order to fix this. You should also consider that leading and trailing whitespace may cause you an issue if it isn't trimmed off. Quote Link to comment https://forums.phpfreaks.com/topic/250926-php-help-asap-please/#findComment-1287333 Share on other sites More sharing options...
joe92 Posted November 11, 2011 Share Posted November 11, 2011 I just tested the alteration of the code that I provided earlier and it does exactly what you want... I enter postcode 'AB12 3CD' and it outputs, 'Postcode1 AB12, <br/> Postcode2 3CD'. I enter postcode 'AB1 3CD' and it outputs 'Postcode1 AB1, <br/> Postcode2 3CD'. FYI, you could make those 5 echo's one echo using the '.' operator to join the sections together: echo "Postcode1 ".substr($postcode, 0, 3)."<br>Postcode2 ".substr($postcode, 3); Quote Link to comment https://forums.phpfreaks.com/topic/250926-php-help-asap-please/#findComment-1287423 Share on other sites More sharing options...
amg182 Posted November 12, 2011 Share Posted November 12, 2011 If you want some form of validation for your postcode consider this one way of doing it. It ensure the correct format is entered i.e Letter Numbers etc....(Uk Postcodes) function IsPostcode($postcode) { $postcode = strtoupper(str_replace(' ','',$postcode)); if(preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode)) return true; else return false; } $codevalid=$postcode; if (IsPostcode($codevalid)) echo "Valid"; else echo "Invalid"; Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/250926-php-help-asap-please/#findComment-1287503 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.