You may use sprintf to pad your strings with spaces and then replace spaces with
(provided that you use a monospace font):
<select name="dd1" class="form-control" id="dd1">
<option value="" disabled="disabled" selected>Select...</option>
<?php
$widthLeft = 30; // width in symbols
$widthRight = 30; // width in symbols
$separator = '|';
while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) {
$optionFormatted = sprintf("%-{$widthLeft}s{$separator}%-{$widthRight}s", $row['PName'], $row['AName']);
$optionHtml = str_replace(' ', ' ', $optionFormatted);
?>
<option value="<?php echo $row['ID'] ?>"> <?php echo $optionHtml ?> </option>
<?php
}
?>
</select>
More solutions:
Update:
For multibyte strings you may use the following function:
<?php
function mb_strpad(string $input, int $pad_length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = 'UTF-8'): string
{
$diff = $pad_length - mb_strlen($input, $encoding);
if ($diff <= 0) {
return $input;
}
$pad = str_pad('', $diff, $pad_string);
if ($pad_type === STR_PAD_LEFT) {
return $pad . $input;
}
return $input . $pad;
}
So that your code will look like this:
<select name="dd1" class="form-control" id="dd1">
<option value="" disabled="disabled" selected>Select...</option>
<?php
$widthLeft = 30; // width in symbols
$widthRight = 30; // width in symbols
$separator = '|';
while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) {
$optionFormatted = mb_strpad($row['PName'], $widthLeft)
. $separator
. mb_strpad($row['AName'], $widthRight);
$optionHtml = str_replace(' ', ' ', $optionFormatted);
?>
<option value="<?php echo $row['ID'] ?>"> <?php echo $optionHtml ?> </option>
<?php
}
?>
</select>