Luca Filosofi
Luca Filosofi

Reputation: 31173

php - regex- preg_replace - space after line-break!

still on regex! i want learn it but i'm still crashing the head into my keybord! ;-)

ok very trivial for you, i'm sure!

Assuming i have this sting, the \s is where the space actualy is... \n where linebreak is..

EDITED:

   OTHERFIELD: Other text here...`\n`
   DESCRIPTION: The quick brown fox jum`\s\n`
   `\s`ps over the lazy dog
   OTHERFIELD: Other text here...`\n`

just for explanation:

each line always start with an UPPERCASE word followed by a colon!

so the only way i have for split each line is by it's last \n for this reason i can't remove it!

then i'm preg_splitting each cleaned line with this regex

/$\R?[^A-Z:]*/m

that give me an array like this:

[DESCRIPTION] => The quick brown fox jumps over the lazy dog

now, what i need to do is remove All the space after the A-Z:

that i have achieved by this regex: /\s+(?![A-Z:])/m that produce this result

DESCRIPTION: The quick brown fox jum ps over the lazy dog

as you can see it leave the space between jum and ps

how to have a result like this?

DESCRIPTION: The quick brown fox jumps over the lazy dog

thank's for the time!

Upvotes: 3

Views: 1971

Answers (1)

Gumbo
Gumbo

Reputation: 655239

Try this regular expression:

/\s+\n\s+/

This will match the whitespace only if it’s surrounding a line feed character. You may need to adjust the quantifiers to fit your actual data.

Upvotes: 2

Related Questions