3

I need to clean up strings of text where every letter has a space in between and every space is composed of 3 spaces, for example:

E X C E P T I O N A L   C R E A T I V I T Y   A N D   A  
T A I L O R E D   D E S I G N   E D G E

How would I go about cleaning the text up, ie - converting every triple-space to a single space, and removing the space in between each letter?

Client-side / server side solutions welcome.

4

3 回答 3

5
preg_replace('/(.) /', '\\1', $string);

The regex engine doesn't match substrings that are the result of a replacement, so it will correctly handle triple spaces without needing to special case them.

于 2013-03-06T19:12:25.387 回答
2
str_replace(array('   ', ' ', '%'), array('%', '', ' '), $text);

Just replace % with a character or a string which does not appear in your text.

于 2013-03-06T19:17:23.510 回答
0
$input = <<<_EOI_
E X C E P T I O N A L   C R E A T I V I T Y   A N D   A  
T A I L O R E D   D E S I G N   E D G E
_EOI_;

$patterns = array('/(\w) /', '/ {2,}/');
$replaces = array('$1', ' ');

preg_replace($patterns, $replaces, $input);

// output: 
// EXCEPTIONAL CREATIVITY AND A
// TAILORED DESIGN EDGE
于 2013-03-06T19:25:51.180 回答