Change language

How can I remove all non-printable characters in a string in PHP?

Example :
Input: str = "Geeks for Geeks" Output: Geeks for Geeks 
Note.Symbols newlines (/ n) and tabs (/ t) are not printable characters.Method 1: Using a generic regular expression:There are many regular expressions available. The best solution - remove all non-ASCII characters from the input string, which can be done with this preg_replace.Example :
// PHP program to remove all unprintable
/ / character from a string 
// A string with non-printable characters $str = "Geeks šžfor ÂGee ks" ;  
// Using the preg_replace method to remove all
// non-printable character from string $str = preg_replace ( ’/ [x00-x1Fx80-xFF] /’ , ’’ , $str );  
// Show change line echo ( $str );  
?>
Exit:
Geeks for Geeks 
Method 2: Usethe regular expression print.Another possible solution - use the regular expression print . The regular expression [: print:]means "any printable character" .Example :  

/ / PHP program to remove all non-printable
// character from string 
// A string with an unprintable character $str = " Geeks šžfor ÂGee ks " ;  
// Using the preg_replace method to remove all
// non-printable character from string $str = preg_replace ( ’/ [[: ^ print:]] /’ , ’’ , $str );  
// Show change line echo ( $str );  
?>
Exit:
Geeks for Geeks 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method