470,815 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

removing whitespaces

rj
Hello,

I try to remove whitespaces from a string using the preg_replace function
like

preg_replace("/[\t ][\t ]*/"," ",$string);

This works great, but makes no handles all characters in the same way. I
want to preserve data between double quotes. So,
hello world "It is a sunny... world"
shall be reduced to
hello world "It is a sunny... world"

and should not remove the whitespaces between the quotes.

Can someone help me ?

Thanks in advance

Richard
Feb 27 '06 #1
1 1583
rj wrote:
This works great, but makes no handles all characters in the same
way. I want to preserve data between double quotes. So,
hello world "It is a sunny... world"
shall be reduced to
hello world "It is a sunny... world"

and should not remove the whitespaces between the quotes.


$str = 'hello world "It is a sunny... world"';

function cb($m) {
if (trim($m[1])) {
return $m[1];
}
return ' ';
}
$str = preg_replace_callback('/(\s+|"[^"]+")/','cb', $str);
JW
Feb 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mar Thomas | last post: by
reply views Thread by Prawdziwa Blondynka | last post: by
1 post views Thread by Dave | last post: by
1 post views Thread by asjad | last post: by
6 posts views Thread by mardif | last post: by
6 posts views Thread by mathieu.malaterre | last post: by
2 posts views Thread by rn5a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.