Solved

regular expression to remove control characters but keep newline

Posted on 2006-11-16
1
1,150 Views
Last Modified: 2013-12-12
Hi All,

I have the following regex to remove control characters, but in some case i want to keep the newline character.

$input = preg_replace('!\p{C}!u', '', $input);

Does anyone know how to modify this  to allow for the newlines in the string?

Thanks for the help!

0
Comment
Question by:amagondes
1 Comment
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
Comment Utility
I think there is no such way using the unicode replacement. An alternative might be to first split a string on \n then preg_replace it parts and then reconstruct the string.

Also, to quote the manual:
Matching characters by Unicode property is not fast, because PCRE has to search a structure that contains data for over fifteen thousand characters. That is why the traditional escape sequences such as \d and \w do not use Unicode properties in PCRE.

-r-
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now