Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd whitespace after comma
    primarykey
    data
    text
    <p>I'm having some trouble figuring this out. </p> <p>I have the following CSV string </p> <pre><code>hello world, hello world, hello </code></pre> <p>The middle value has excess whitespaces. I'm trimming that using</p> <pre><code>preg_replace('/( )+/', ' ', $string) </code></pre> <p>The function is excellent but it removes the whitespaces after the commas as well. It becomes..</p> <p><code>hello world,hello world,hello</code></p> <p>I want to preserve 1 whitespace after commas like so</p> <p><code>hello world, hello world, hello</code></p> <p>How can I do this?</p> <p>EDIT: </p> <p>Using <code>preg_replace('/(?&lt;!,) {2,}/', ' ', $string);</code> as suggested, works but I ran into another issue.. When I use more than 1 whitespace after a comma it return 2 whitespaces after the comma.</p> <p>so</p> <pre><code>hello world, hello world,hello </code></pre> <p>returns</p> <pre><code>hello world, hello world, hello </code></pre> <p>As a solution I create an array from the CSV string and used <code>implode()</code></p> <pre><code>$string = "hello world, hello world,hello"; $val = preg_replace('/( )+/', ' ', $string); $val_arr = str_getcsv($val); //create array $result = implode(', ', $val_arr); //add comma and space between array elements return $result; // Return the value </code></pre> <p>Now I get <code>hello world, hello world, hello</code> It also ensures a whitespace after comma if missing. </p> <p>It seems to work, not sure if there is better way. Feedbacks are welcomed :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload