Re: regex to remove NL and CR

From: Date: Wed, 18 Aug 2004 01:57:14 +0000
Subject: Re: regex to remove NL and CR
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-194116@lists.php.net to get a copy of this message
Hi, Wednesday, August 18, 2004, 9:09:02 AM, you wrote: KB> I am extracting text from a text datatype field of a KB> database. The target text is marked (beginning and KB> end) by html comments that have a known--but not KB> uniform--format. Unfortunately, the web-based KB> interface used to maintain the data inserted hard line KB> breaks in the text. KB> I initially tried removing all NL and CR chars from KB> the string, which has the unanticipated result of KB> removing some spaces between words that had wrapped in KB> the admin interface. KB> Now, I need to remove all NL and CR that occur within KB> a string starting with "<!-" and ending with "->". I KB> have no idea where to start, because the NL and CR KB> could also occur in the strings that mark the start KB> and end of the comments. KB> I am very new to regex, but starting to get it. If KB> someone could just give me a push in the right KB> direction. Nothing I have tested in regex coach comes KB> close to matching what I need. KB> Kathleen This is probably quicker using a callback function like this: $string = "<!-- a\nlong\rtest\n\rstring -->"; echo nl2br(htmlentities($string)); echo '<br>'; function replace($str){ return preg_replace('/\n\r|\n|\r/',' ',$str[0]); } $newstring = preg_replace_callback('/<!--(.*?)-->/s','replace',$string); echo nl2br(htmlentities($newstring)); -- regards, Tom

« previous php.general (#194116) next »