Substrings Help

Hi there, I am new to php and so happy to have found this forum for beginners. I would really apreciate some help. I have done this form, I need to covert lower cases to upper, remove white spaces and trim the outcode. The first two functions work but the other doesn’t. Also can I use two substrings within a string?

Thanks

[php]
echo’

Post Code:

';

$password_file = $include_path.“includes/passwd.inc.php”;
include($password_file);
include $include_path.“banemails.inc.php”;

//-- Database connection
$link = mysql_connect($DbHost, $DbUser, $DbPassword);

@mysql_select_db(babrrffv_auct1) or die( "Unable to select database");

if (isset($_POST[‘sub1’])) {
$outcode = $_POST[‘sub1’];

//-- Trim Postcode
$outcode = substr ($outcode,0,strlen ($outcode)-3));
//-- Convert lowercase to uppercase, remove spaces
$outcode = strtoupper(str_replace (" “,”", $outcode);

}
echo $outcode;
[/php]

Hi phplady, welcome to the forum!

I looked at your code and hope the following will be of help to you.

Your parentheses are not balanced in a couple of lines.

Try changing this:[php]$outcode = substr($outcode,0,strlen($outcode)-3));[/php]
to this…[php]$outcode = substr($outcode,0,-3);[/php]
Note that you can use the strlen if you want, but it is unnecessary and simply using -3 does the same thing. I want to make sure that you intend to remove the last three characters that were put into the sub field on your form. If you intended to remove leading and trailing whitespace, you would use this instead…[php]$outcode = trim($outcode);[/php]

Also change this:[php]$outcode = strtoupper(str_replace (" “,”", $outcode);[/php]
to this…[php]$outcode = strtoupper(str_replace(" “,”",$outcode));[/php]

Also, you have an unclosed html paragraph in your form and you are closing your body element too early (you are echoing $outcode after your body close).

A couple of other comments: I am assuming that you have removed some code, as you are connecting to your database, but not doing anything with the connection. Nothing in the code you posted should require a database connection. I have removed this (as well as your includes from my example, as none of them are needed for the code that you posted.

Your echo $outcode should be inside your isset. It will throw a warning (or notice) if you leave it where it is and the isset condition isn’t met.

The last thing to be aware of is that it is perfectly acceptable to echo your form the way you are. It is often easier to read the code; however, if you put sections of pure HTML such as this outside of your php tags (and remove the echo, single-quotes, and semi-colon.

I am including a complete example so that you can see how all three of these changes looks. I removed the database query as well, but you could insert it should it be part of some un-posted code. Note that the code I am including is HTML5 and contains all elements that you should have as part of your page. Unless you have a reason to use something else, I would use HTML5. Also, I am assuming that this file is “calculator.php”? If so, with HTML5, the proper way to post to the same page is to leave the action attribute out entirely. If not, you will want to add the action back in.

[php]

Calculator Demo

Post Code:

<?php if (isset($_POST['sub1'])) { $outcode = $_POST['sub1']; $outcode = substr($outcode,0,-3); // Trim Postcode $outcode = strtoupper(str_replace (" ","", $outcode)); // Convert lc to uc & remove spaces echo $outcode; } ?> [/php]

Please look this over and let me know if you have any questions.

Best,

jay

Hi Jay,

First I want to thank you for taking the time to help me writing so much information for me. You are a star!
It works fine now and I understand the errors I made. :wink:

Love

phplady

Glad to hear it!

Let us know anytime you have questions! Its a great community and given enough information to understand the problem, someone here almost always has the answer.

Best,

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service