-2

I have a string:

Some string, "it's a nice string". I like it. "some other text"

I want remove spaces, excluding there beetween ":

Somestring,"it's a nice string".Ilikeit."some other text"

How I can goal this?

3
  • 1
    By processing the string and removing the spaces. See PHP string functions and regular expressions. Commented Dec 21, 2011 at 11:55
  • What have you tried so far? You're getting down voted for not showing any effort to do this I believe. Commented Dec 21, 2011 at 12:18
  • Yeah, I tried regular expressions but I can only remove all spaces, I have no idea how to ommit there between tags Commented Dec 21, 2011 at 12:45

3 Answers 3

1

You could use regular expressions, or you could cheat and use explode():

$text_before = 'Some string, "it\'s a nice string". I like it. "some other text"';
$text_after = array();
$text_quotes = explode('"', $text_before);
for ($i = 0, $max = count($text_quotes); $i < $max; $i++) {
    if (($i % 2) == 1) {
        $text_after[] = $text_quotes[$i];
    } else {
        $text_after[] = str_replace(' ', '', $text_quotes[$i]);
    }
}
echo implode('"', $text_after);
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe, but your solution is cleaner :)
0

You may achieve it by using php str_replace function. Please check http://php.net/manual/en/function.str-replace.php

Comments

0

I'm not good with regex, so this solution doesn't use any. What I would do:

$str = 'Some string, "it\'s a nice string". I like it. "some other text"';
$pieces = explode('"', $str);
for($i = 0; $i < count($pieces); $i += 2){ // Every other chunk is quoted
    $pieces[$i] = str_replace(' ', '', $pieces[$i]);
}
$str = implode('"', $pieces);

If the string starts with double quotes, php will make the first element of the $pieces array empty, so this should still work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.