0

I'm using the google translate api for some simple stuff but when translating english to other languages it sometimes gives me spaces between quotes, so can someone give me a regex matching statement in php to replace the space between the quote and first word and the quote and last word?

Example translated phrase: word word word " constructie in Londen " word word word

I would like the regex to convert it to: word word word "constructie in Londen" word word word

Thanks!

3
  • Is it guaranteed that there will be no unmatched quotes? Commented Feb 14, 2011 at 3:52
  • not sure what you mean, some phrases might have the quotes, some phrases might not. So if there is not matched quotes then obviously the regex wouldn't do anything to the string. Commented Feb 14, 2011 at 3:55
  • I am saying are there unbalanced quotes. Ie in the whole text string are there an odd number of quotes instead of even. Commented Feb 14, 2011 at 4:20

1 Answer 1

1

This is the pattern: "\s*(.*?)\s*"

$str = 'word word word " constructie in Londen " word word word';
$newStr = preg_replace('/"\s*(.*?)\s*"/', '"\\1"', $str);
echo $newStr;
// word word word "constructie in Londen" word word word

This will also work with multiple quoted segments:

$str = 'word word word " constructie in Londen " word word wordword word word " constructie in Londen " word word wordword word word " constructie in Londen " word word word';
$newStr = preg_replace('/"\s*(.*?)\s*"/', '"\\1"', $str);
echo $newStr;
// word word word "constructie in Londen" word word wordword word word "constructie in Londen" word word wordword word word "constructie in Londen" word word word

Or you could use the /e modifier with trim:

$str = 'word word word " constructie in Londen " word word wordword word word " constructie in Londen " word word wordword word word " constructie in Londen " word word word';
$newStr = preg_replace('/"(.*?)"/e', "'\"'.trim('\\1').'\"'", $str);
echo $newStr;
// word word word "constructie in Londen" word word wordword word word "constructie in Londen" word word wordword word word "constructie in Londen" word word word

Edited to use Phil Brown's suggestion.

Edited to use Alan Moore's suggestion.

Sign up to request clarification or add additional context in comments.

4 Comments

You can also use a non-greedy match, rather than the "everything but quotes" range, eg /"\s?(.*?)\s?"/
@Phil, I'm not sure why that works, but it does, and it's much cleaner. Bravo. I'm gonna have to google that one.
With that trim() call in the replacement, you don't need to match the spaces explicitly; '/"(.*?)"/e' works just as well, as does '/"([^"]*)"/e'. Or you could drop trim() and take full advantage of reluctant quantifiers instead: $newStr = preg_replace('/"\s*([^"]*?)\s*"/', '"$1"', $str); - ref: regular-expressions.info/repeat.html
@Alan: you're correct. The match was originally different, which did require the \s?s, but was updated thanks to Phil Brown's comment. It can be streamlined a lot now. I will update it again to reflect your input.

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.