Special characters throwing off str_pad in php?

Active3 hr before
Viewed126 times

5 Answers


Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,This is why you are missing three spaces, str_pad is counting 'Ã' as 4 single byte characters instead of one multi-byte one.,This is happening because 'Ã' is a multi-byte character (4 bytes long), and str_pad is counting bytes rather than logical characters., Stack Overflow help chat

Try this function (credit here).

< ?
function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT) {
   $diff = strlen($input) - mb_strlen($input);
   return str_pad($input, $pad_length + $diff, $pad_string, $pad_type);
} ?
load more v

str_pad — Pad a string to a certain length with another string, The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length. , If the value of length is negative, less than, or equal to the length of the input string, no padding takes place, and string will be returned. , Returns the padded string.

load more v

? php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6, "___"); // produces "Alien_"
echo str_pad($input, 3, "*"); // produces "Alien"
load more v

There are four ways to write a string literal in your PHP code: using single quotes, double quotes, the here document (heredoc) format derived from the Unix shell, and its “cousin” now document (nowdoc). These methods differ in whether they recognize special escape sequences that let you encode other characters or interpolate variables.,Double-quoted strings interpolate variables and expand the many PHP escape sequences. Table 4-1 lists the escape sequences recognized by PHP in double-quoted strings.,The htmlspecialchars() function converts the smallest set of entities possible to generate valid HTML. The following entities are converted:,Each substitution marker in the template consists of a percent sign (%), possibly followed by modifiers from the following list, and ends with a type specifier. (Use %% to get a single percent character in the output.) The modifiers must appear in the order in which they are listed here:

The simpler of the two ways is to put the variable name in a double-quoted string or in a heredoc:

$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here

The other way is to surround the variable being interpolated with curly braces. Using this syntax ensures the correct variable is interpolated. The classic use of curly braces is to disambiguate the variable name from any surrounding text:

$n = 12;
echo "You are the {$n}th person";
You are the 12 th person

Unlike in some shell environments, in PHP, strings are not repeatedly processed for interpolation. Instead, any interpolations in a double-quoted string are processed first and the result is used as the value of the string:

$bar = 'this is not printed';
$foo = '$bar'; // single quotes
load more v

Note that in the code above, we leveraged the stringr consistency and used the pipe.,In general, spaces at the start or end of the string are uninformative. These can be particularly deceptive because sometimes they can be hard to see:,This pattern now only detects the strings with exactly one digit:,The code above shows the first step in constructing the dataset

load more v

Other "special-characters" queries related to "Special characters throwing off str_pad in php?"