Skip to content
Advertisement

PHP / Linux shell unwanted character

I have an HTML form (field with multiple lines) that feeds a PHP code that will read each line of the field, and generate a pdf file using the line string as name. My problem is that all but the last file have a trailing ‘?’ at the end of the file. I think that somehow the form is sending an special character to the variable and I can’t seem to remove it.

Here is a snippet of the code:

HTML:

<html>
<body>

<form action="mkpdf.php" method="post">
  <p>Students&rsquo; name</p><br><textarea name="names" cols="40" rows="5"></textarea>
  <p>Students&rsquo; email</p><br><textarea name="emails" cols="40" rows="5"> </textarea>
  <br><br><input type="submit" value="Submit">
</form>

PHP:

<?php

require ('fpdf/fpdf.php');
require ('PHPMailer-master/PHPMailerAutoload.php');


if (isset ($_POST["names"]) && ($_POST["emails"])) {
  exec('cd out ; rm *');
  $date = date('F , jS');
  $names = trim($_POST["names"]);
  $emails = trim($_POST["emails"]);
  $NamesAr = preg_split('/[n]+/', $names);
  $EmailsAr = preg_split('/[n]+/', $emails);
  foreach ($NamesAr as $name) {
    foreach ($EmailsAr as $email) {
  ...
  PDF commands
  ...
     $pdf->Output("out/$name.pdf");
  }
  }

OK.. So if in the Form I type:

Number One
Number Two
Number Three

Run the script.. When I list the “out” folder this is what I get:

Number One?.pdf
Number Two?pdf
Number Three.pdf

Question is, how do I remove this special character from the file names?

Thanks

Advertisement

Answer

most likely you’re passing “rn” linebreaks when submitting the form, so when you do a preg_split('/[n]+/', $emails); it leaves r symbol there, which is not printable, hence the ? character

To fix this, use rn in preg_split

$NamesAr = preg_split('/[rn]+/', $names);
$EmailsAr = preg_split('/[rn]+/', $emails);
Advertisement