Avatar of Joseph Longo
Joseph Longo
Flag for United States of America asked on

Create an Array Which Holds Each Character of Input

Hello Experts,

I have an HTML form, which creates a PDF file upon submission.

Part of my form is as follows:

Driver's License Number:
<br><br><input type="text" id="dlNumber" name="dlNumber">
I need each character of the driver's license number to be written at a pre-defined location. Therefore, I need to be able to write each character separate from one another.

Would I use the:
explode();
var_dump(); or
array(); functions or
A combination of all three to achieve this? Or could I just use str_split?
HTMLPHP

Avatar of undefined
Last Comment
Joseph Longo

8/22/2022 - Mon
Dave Baldwin

None of those.  A string is already defined as a character array.  Just access each character with array syntax.
for ($i = 1; $i < strlen($str); $i++) {
$nextchar = $str[$i];
// do something with $nextchar
}

Open in new window

https://www.php.net/manual/en/language.types.string.php#language.types.string.details
Joseph Longo

ASKER
First off, thank you for your reply. I really like your example, as it counts how many characters the string contains which is something I did not take into consideration. I am just a little confused how I would be able to write each character in a different location, separate from the other. I am not sure if this is considered "good" php, as I am somewhat of a newbie to php and programming in general. However, I was able to achieve my goal, using something like this:

$dl="L123456789012";
$dlSplit =(str_split($dl,1));
$pdf->Text(34, 94, $dlSplit[0]);
$pdf->Text(45, 94, $dlSplit[1]);
$pdf->Text(56, 94, $dlSplit[2]);
Doing so, allowed me to write each character in a different spot than the other.



Dave Baldwin

That looks fine.  When you run out of characters, $dlSplit[x] is going to cause an error for a non-existent index 'x'.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Joseph Longo

ASKER
I really like your code example. As stated before, I am new to programming, so I apologize ahead of time for my naiveness. If I wanted to implement your code, would this work:
for ($i = 1; $i < strlen($str); $i++) {
$x = 34;
$y =94;
$nextchar = $str[$i];
// do something with $nextchar
$pdf->Text($x, $y, $nextchar);
$x = $x + 11;
   if(strlen($str)> 15){
      $x=34;
      $y= 100;
      $pdf->Text($x, $y, $nextchar);
      $x = $x + 11;
  }
}

Basically, I would need to move the text along the X-axis by 11. If the string length is greater than 15 (which would be more than the number of boxes on the first line), I would need to move the text down the Y-axis by 6 and then move the text along the X-axis by 11.
Dave Baldwin

This section is going to reset the position every time.  
 if(strlen($str)> 15

Open in new window

You may need to move that to before the loop.
Joseph Longo

ASKER
But, if the strlen is greater than 15, I need the text to shift on the Y-axis by a certain amount and then continue writing the text. So, if I put it before the for loop, will the text get written?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

Where it is, the character will be written to the same position every time because you are resetting the position on every pass thru the loop.  Have you tried any of this yet?  Either it works or you learn.
ASKER CERTIFIED SOLUTION
Joseph Longo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.