InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP regex

  • preg_match online
  • preg_match_all online
  • preg_replace online
  • Regex OR (alternation)
  • beginning and end of string
  • look ahead and look behind
  • preg_match - greedy and lazy
  • preg_replace - optional char group
  • preg_replace - optional one char
  • preg_replace back reference (in replacement text)
  • preg_replace – back reference within pattern
  • preg_replace – dot all
  • regex delimiters
  • regex shorthand - digit (\w)
  • regex shorthand - whitespac (\s)
  • regex shorthand - word boundary (\b)
  • regex shorthand - word character (\w)
  • remove non printable chars
 
  • Home
  • > Tutorials
  • > PHP
  • > PHP Regex

PHP remove non printable characters from a string

By admin on Jan 6, 2016

Regex to remove non printable characters from a string

Space is first printable char and tilde (~) is last printable ascii char. We replace any char which does not fall in that range.

<?php
$str = "Hello\n\tWorld";
$newstr = preg_replace('/[^ -~]/', "", $str);
var_dump($str);
var_dump($newstr);
?>
string(12) "Hello
	World"
string(10) "HelloWorld"
Env: PHP 7.3.18 (Linux)

Regex to replace non printable characters and space and collapse multiple spaces

Replace multiple occurences of chars which donot not fall in range (! to ~) with one space. Regex by default is greedy.

<?php
$str = "Hello\n\tWorld";
$newstr = preg_replace('/[^!-~]+/', " ", $str);
var_dump($str);
var_dump($newstr);
?>
string(12) "Hello
	World"
string(11) "Hello World"
Env: PHP 7.3.18 (Linux)

Suggested posts:

  1. PHP – take last n characters of a string
  2. PHP regex – whitespace shorthand (\s) regex examples
  3. PHP beginning and end of string regex examples
  4. PHP – split string examples
  5. Perl command line – replace multi line comments
  6. Python rstrip – remove training spaces and newline
  7. PHP remove trailing whitespaces and newline
  8. Python range examples
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PHP, PHP Regex, preg_replace, Tutorials

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress