removing whitespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rj

    removing whitespaces

    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


  • Janwillem Borleffs

    #2
    Re: removing whitespaces

    rj wrote:[color=blue]
    > 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.
    >[/color]

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

    function cb($m) {
    if (trim($m[1])) {
    return $m[1];
    }
    return ' ';
    }
    $str = preg_replace_ca llback('/(\s+|"[^"]+")/','cb', $str);


    JW


    Comment

    Working...