PHP - Trim() With Numbers With Leading Zeros, Possible Bug
Discovered something very strange with trim() and numbers with leading zeros.
Code: [Select] <?php $number = 0456; echo trim($number); ?> That ouputs 302. How the heck does 0456 trimmed turn into 302? Is this a bug? Similar TutorialsI have a db with entries as follows: A001 A002 A003 I would like to loop thru these and print them on screen. Instead of doing: echo A001 echo A002 echo A003 I'd like to find a better way since this database will scale up in the future and may get to A999. Is there a way to increment with leading placeholder zeros? for($x=0 ; $x < mysql_num_rows($myresult) ; $x++){ echo $row['A'.##increment here##']; } Thanks! Sorry if this has already been solved. Also, I apologize if this is an amateur question... I have a simple form and a script to do some validating of user data, among other tasks. One part of the script is intended to verify a birthday. Code: [Select] $x = $_POST['x']; $x = trim($x); According to the php.net manual, trim() is supposed to remove whitespace before and after a string. My intent by using trim() is to determine if someone tried to skip the birthday field. I have other validation running on the value of $x, but I wanted trim() to remove whitespace. However, the value of x, which in this case is expected to be numeric, such as 12 for a month, ends up being 1. This happens with any 2-digit number that a user might enter into the form field for x. The second number is always removed. Any insight into why trim() removes the second number, 2 in the above example, leaving only a "1" as the value of $x, would be very greatly appreciated. Thanks in advance. I have a form that is posting a value from a radio button as well as 16 Hidden values. The form action goes to file2- which is successfully conecting and processing but instead of inserting the correct values I'm just seeing a row of zeros. I don't know where to start looking! This is the code from the posting form : --- <?php // query.php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); $result = mysql_query("SELECT qid,question,oc1,oc2,oc3,oc4,oc5,oc6,oc7,psc1,psc2,psc3,psc4,psc5,psc6,psc7,psc8,psc9 FROM tblquestions WHERE qid = '1'"); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } $row = mysql_fetch_row($result); echo $row[0]; // Question Number echo $row[1]; // Question echo $row[2]; // oc1 echo $row[3]; // oc2 echo $row[4]; // oc3 echo $row[5]; // oc4 echo $row[6]; // oc5 echo $row[7]; // oc6 echo $row[8]; // oc7 echo $row[9]; // pcs1 echo $row[10]; // pcs2 echo $row[11]; // pcs3 echo $row[12]; // pcs4 echo $row[13]; // pcs5 echo $row[14]; // pcs6 echo $row[15]; // pcs7 echo $row[16]; // pcs8 echo $row[17]; // pcs9 ?> <p>Hello </p> <?php echo $row[0]; // Question Number ?> <form id="form1" name="form1" method="post" action="./uinputprocess.php"> <table width="800" border="0"> <tr> <td colspan="6"><p>Question <?php echo $row[0]; // Question Number ?> </p> <?php echo $row[1]; // Question ?> .</td> </tr> <tr> <td width="126"><p align="center"> </p> <p align="center">Strongly Disagree </p></td> <td width="65"><p align="center"> </p> <p align="center">Disagree </p></td> <td width="145"><p align="center"> </p> <p align="center">Unsure but Doubtful </p></td> <td width="152"><p align="center"> </p> <p align="center">Unsure but Probable </p></td> <td width="67"><p align="center"> </p> <p align="center">Agree </p></td> <td width="219"><p align="center"> </p> <p align="center">Strongly Agree </p></td> </tr> <tr> <td><div align="center"> <input type= "radio" name= "ans" value = "1" /> </div></td> <td><div align="center"> <input type= "radio" name= "ans" value = "2" /> </div></td> <td><div align="center"> <input type= "radio" name= "ans" value = "3" /> </div></td> <td><div align="center"> <input type= "radio" name= "ans" value = "4" /> </div></td> <td><div align="center"> <input type= "radio" name= "ans" value = "5" /> </div></td> <td><div align="center"> <input type= "radio" name= "ans" value = "6" /> </div></td> </tr> <tr> <td colspan="6"> </td> </tr> <tr> <td colspan="6"><p>Hidden Fields Here > <input type="hidden" name="oc1" id= <?php "echo $row[2];" ?> /> <input type="hidden" name="oc2" id= <?php "echo $row[3];" ?> /> <input type="hidden" name="oc3" id= <?php "echo $row[4];" ?> /> <input type="hidden" name="oc4" id= <?php "echo $row[5];" ?> /> <input type="hidden" name="oc5" id= <?php "echo $row[6];" ?> /> <input type="hidden" name="oc6" id= <?php "echo $row[7];" ?> /> <input type="hidden" name="oc7" id= <?php "echo $row[8];" ?> /> <input type="hidden" name="psc1" id= <?php "echo $row[9];" ?> /> <input type="hidden" name="psc2" id= <?php "echo $row[10];" ?> /> <input type="hidden" name="psc3" id= <?php "echo $row[11];" ?> /> <input type="hidden" name="psc4" id= <?php "echo $row[12];" ?> /> <input type="hidden" name="psc5" id= <?php "echo $row[13];" ?> /> <input type="hidden" name="psc6" id= <?php "echo $row[14];" ?> /> <input type="hidden" name="psc7" id= <?php "echo $row[15];" ?> /> <input type="hidden" name="psc8" id= <?php "echo $row[16];" ?> /> <input type="hidden" name="psc9" id= <?php "echo $row[17];" ?> /> </p> <p>Pin <label for="textfield"></label> <input name="textfield" type="password" id="textfield" size="20" maxlength="5" /> </p></td> </tr> <tr> <td colspan="6"><p> <input type="submit" name="button" id="button" value="Submit" /> </p> <p> </p></td> </tr> </table> </form> And this is the code from the processing file <?php // query.php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); $pin =$_POST['pin']; $qid =$_POST['qid']; $oc1 =$_POST['oc1']; $oc2 =$_POST['oc2']; $oc3 =$_POST['oc3']; $oc4 =$_POST['oc4']; $oc5 =$_POST['oc5']; $oc6 =$_POST['oc6']; $oc7 =$_POST['oc7']; $psc1 =$_POST['psc1']; $psc2 =$_POST['psc2']; $psc3 =$_POST['psc3']; $psc4 =$_POST['psc4']; $psc4 =$_POST['psc5']; $psc6 =$_POST['psc6']; $psc7 =$_POST['psc7']; $psc8 =$_POST['psc8']; $psc9 =$_POST['psc9']; $ans =$_POST['ans']; $enter_sql= "INSERT INTO tblresults (pin,qid,oc1,oc2,oc3,oc4,oc5,oc6,oc7,psc1,psc2,psc3,psc4,psc5,psc6,psc7,psc8,psc9) VALUES ('$pin','$qid','$oc1','$oc2','$oc3','$oc4','$oc5','$oc6','$oc7','$psc1','$psc2','$psc3','$psc4','$psc5','$psc6','$psc7','$psc8','$psc9')"; $enter_query =mysql_query($enter_sql) or die (mysql_error()); ?> <body> <p>Thank you - You have successfully entered your answer</p> hi, I would like to add a leading 0 to my FOR loop for ($i=0; $i<23; $i++) if($i<10){ $i = 0$i; } { echo "<option value=".$i.">" . $i . "</option>"; } this gives and error: unexpected T_VARIABLE at the moment I have: 0 1 2 3 4 5 6 7 8 9 I would like: 00 01 02 03 04 05 06 07 08 09 Looking for the best method to conditionally strip leading zeros for the following situation: $a = array(01, 02, 03, ... 10, 11); $a < 10 ? $a = ? : ''; For the following result: 1, 2, 3, ... 10, 11 Is there a better method than explode? Ok my question is, when they hit the "Play!" button, then I want the php code below to pop up. Like... i think I would use the GET method, but I'm not sure still. I don't think that really matters what method you use, but how do I say IF they click on the "Play" button, then lead them to this code so they can start playing the game. I'm entering this into the database, but I'm just trying to get this code first. I already know how to make it lead to the database and whatnot. <?php session_start(); include("logincheck.php"); ?> <?php include_once("header.php"); ?> <br> Welcome to the Six Dice game. <br><br><b>Instructions</b>: You play this game by randomly rolling a dice. If you land on 1-5, you earn 0 rp. If you land on a 6, you earn 500rp! The game is free so why not give it a shot? <br>You may only play this game 25 times a day! <br><br><form action="sixdice.php?roll" method="post"> <input type="submit" name="playsix" value="Play!" /> </form> <?php if(isset($_POST['playsix'])); $dice = rand(1,6); echo "You rolled a<br /><b>{$dice}</b>\n"; $winnings = "500"; if($dice == 6) { include("haha.php"); $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase); $username = $cxn->real_escape_string($_SESSION['username']); $sql = "UPDATE `Member` SET `rp` = rp+$winnings WHERE `username` = '$username'"; mysqli_query($cxn,$sql); }?> <FORM ACTION="sixdice.php" METHOD="post"> <INPUT TYPE="submit" VALUE="Roll Again!" /> </FORM> <?php include_once("footer.php"); ?> Hello, It's as subject says. Here is the code: <? //Config include_once "../../config/config.php"; //Variables $username = $_POST["username"]; $username = mysql_real_escape_string($username); $password = $_POST["password"]; $password = md5($password); //Check for User $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $query = mysql_query($query); //If user not found if(mysql_num_rows($query)!="1") { echo "No User Found"; } //If User Found else { while($row=mysql_fetch_array($query)) { $fname = $row["fname"]; $lname = $row["lname"]; $userid = $row["id"]; $admin = $row["admin"]; } setcookie("fname", "$fname"); setcookie("lname", "$lname"); setcookie("userid", "$userid"); setcookie("admin", "$admin"); header('Location: ../../../portal/index.php'); } ?> On the page it forwards to, I write print_r($_COOKIE); Yet it doesn't show my newly set cookies, yet the above snipped does on the script code above. George Hi, I need to sort variables in groups of up to 15 and put it in an array. For example: $exstract['center_tabOpBody_0'] =5 $exstract['center_tabOpBody_1'] =6 $exstract['center_tabOpBody_2'] =8 $exstract['center_tabOpBody_3'] =1 Should yield: ARRAY( = center_tabOpBody_1,center_tabOpBody_2,center_tabOpBody_3 // <-----15 [1] = center_tabOpBody_0 //<----5 ) Is there some simple function do do the "efficiency" sort? Thanks, Vadim Hello, I have been messing around with this and can't seem to figure out what I'm doing wrong. I'm pretty new to php, so it might be something silly. Original code: Code: [Select] $a1=mysql_result($result,$i,"question"); //Question $a2=mysql_result($result,$i,"answer"); //Answer $a3=mysql_result($result,$i,"submission_id"); //submission id $a4=mysql_result($result,$i,"category"); //submission id echo "<a name=\"$a3\"></a><strong>$a1</strong>$a2"; Prints: Question Category What I want is: Question Category I have tried using the trim functions but nothing seems to help: Code: [Select] $a1=mysql_result($result,$i,"question"); //Question $a2=mysql_result($result,$i,"answer"); //Answer $a3=mysql_result($result,$i,"submission_id"); //submission id $a4=mysql_result($result,$i,"category"); //submission id $a1 = rtrim($a1); $a2 = ltrim($a2); echo "<a name=\"$a3\"></a><strong>$a1</strong>$a2"; Ideas? ive used trim to get rid of the end of a a string and it takes an extra character off it anyone can help please Code: [Select] $class_name = "League_Model"; echo $model_name = trim($class_name, "_Model"); result: Leagu im want it to return: league I'm using the following code to attempt to strip white space from my search results: Code: [Select] while($row = mysql_fetch_assoc($rs)) { trim($row['Name']); trim($row['Address']); trim($row['City']); trim($row['State']); trim($row['Zip']); trim($row['Phone']); echo "<td>" . $row['Name'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['State'] . "</td>"; echo "<td>" . $row['Zip'] . "</td>"; echo "<td>" . $row['Phone'] . "</td>"; echo "<td>" . $row['Type'] . "</td>"; Obviously, I'm leaving out a ton of code, but the important part is that it's not stripping the white space. Things like this make me want to pound spikes into my eyes, because there's just no logical way for me to work though this. I'm extremely OCD, and so if code doesn't work the way the documentation states, I freeze. I also tried (as per another tutorial example) setting a second variable trimming it, and using the second variable in the Echo, like so: Code: [Select] $Tname = ($row['Name']); echo "Blah blah blah" . trim($Tname); That didn't work either. It's simply NOT removing the white space, despite the fact that the entire description of what "Trim" does consists of "removes white space." It boggles my mind. Am I supposed to sacrifice a goat first or something? BTW, if I go in and edit the database manually, I can strip out the spaces by hand, and everything works fine. But it seems to me like the Trim command is supposed to exist to make it so that I don't have to do that. The only thing I can guess is that for some reason the contents of the DB aren't being seen as Strings, even though they are, in fact, Strings. I'm able to maniupulate them as Strings everywhere else in the code. Just not here. Does anyone know what I did wrong? Thanks! Kyle i have a function that trims all post data $trimmed = array_map('trim', $_POST); however because i am subminting a post array like [locations] => Array ( [0] => Oldham [1] => Royton [2] => Delph [3] => Cheadle ) it gives me an error saying An error occurred in script 'C:\xampp\htdocs\admin\create_listing.php' on line 23: trim() expects parameter 1 to be string, array given how can i stop this all other data is posted in a string however the locations one is posted in an array $string = " hel lo "; $string = rtrim($string); $string= ltrim($string); which trims the whitespace from the front and back, but how do i remove the middle space? Thanks Hi
Using: echo $row['columnname'];
How can I strip off data from the above echo? The column always begins with 'Local/' then 3 digits then other unwanted data, lke 'Local/567@context-0000461d;1'.
It would be ideal to just display '567'.
Hello, I am trying to use the php function string substr ( string $string , int $start [, int $length ] ) to remove trailing zero's in the DB from the price displayed on the page. Currently using the code below I get this for output - $27.9500 <?php echo $row['prodprice']; ?> I would like to get this $27.95. So I tried using the string substr ( string $string , int $start [, int $length ] ) like this - <?php echo substr("$row['prodprice']",0,5); ?> but I receive t string errors and such using variations of this. Can someone please explain to me what I am doing wrong and give me some pointers on how to fix this? Thanks Is there a way to trim everything off in front of the last backslash in the string? Code: [Select] $trimFile = $fList[$i]; Example: In this path: \\\\myftp/downloads/tom/work/word_docs\\legal\test.pdf I only want to display test.pdf. Thanks, Jake Hi - wondering if someone can give a little guidance: Why doesn't this trim the whitespace out of the string postzip? trim(strtoupper(((isset($_POST["postzip"]))?$_POST["postzip"]:""))) Thanks Code: [Select] <label for="first_name">* First Name </label> <input type="text" name="first_name" maxlength="64" value=<?php echo formatPhone(clean_input($_POST['first_name']); ?>> I have a form where I want to enter a first name. I want the fields to take only alphabets(no numbers). Also when a user enters something like John Doe separated by multiple white spaces, I want it to separate the two words only by a single space. I am not sure how this is done in php. Is there a way to apply the function trim() on a whole array, instead of applying it to the individual input fields one by one? Here's the code: $blurb1_title = array ( $_POST['blurb1_title1'], $_POST['blurb1_title2'], $_POST['blurb1_title3'], $_POST['blurb1_title4'], $_POST['blurb1_title5'] ); $blurb2_title = array ( $_POST['blurb2_title1'], $_POST['blurb2_title2'], $_POST['blurb2_title3'], $_POST['blurb2_title4'], $_POST['blurb2_title5'] ); $blurb3_title = array ( $_POST['blurb3_title1'], $_POST['blurb3_title2'], $_POST['blurb3_title3'], $_POST['blurb3_title4'], $_POST['blurb3_title5'] ); All those $_POST variables are input fields, and I would like to apply trim() to them so the spaces get removed. Is there an efficient way to do it without having to apply it all individually, if yes, what would be the correct syntax? [php] Hey Is there a way to trim the print_r($array); so its completed and void of white space? It prints like this: Code: [Select] Array ( [0] => Array ( [id] => 10 [f] => data/tiles/image.png ) ) But I want it to be compressed like this: Code: [Select] Array([0]=>Array([id]=>10[f]=>data/tiles/image.png)) This is then to be stored in the database, so any compressed possible is a plus in this situation. Any ideas? |