user1981730
user1981730

Reputation: 89

PHP remove all spaces after specific character

I have a text file that looks something like this:

What word is on page 9, 4 words from the left on line 15?   account
What word is on page 11, 2 words from the left on line 5?   always

Is there a way that I can remove all of the spaces after the "?" so it looks something like this (the space amounts are all different throughout the actual text file):

What word is on page 9, 4 words from the left on line 15?account
What word is on page 11, 2 words from the left on line 5?always

Upvotes: 1

Views: 1877

Answers (2)

John Conde
John Conde

Reputation: 219804

$input = 'What word is on page 9, 4 words from the left on line 15?   account';
echo preg_replace('/\?\s+/', '?', $input);

See it in action

Upvotes: 1

Tchoupi
Tchoupi

Reputation: 14681

Use a regular expression:

$str = 'What word is on page 9, 4 words from the left on line 15?   account';
echo preg_replace('/\?\s+/', '?', $str);

Upvotes: 3

Related Questions