php string strip ascii characters code example

Example 1: php remove last character in string

//Remove the last character using substr
$string = substr($string, 0, -1);

Example 2: php substr remove last 4 characters

echo substr($string, 0, -3);

Example 3: php regex non printable characters

myprompt> php -a
Interactive shell

php > $string = "‘Hello,’ she said.";
php > $result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
php > echo $result;
Hello, she said.

Example 4: php regex non printable characters

$result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);

Tags:

Php Example