开发者

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

开发者 https://www.devze.com 2022-12-28 17:08 出处:网络
still on regex! i want learn it but i\'m still crashing the head into my key开发者_开发问答bord! ;-)

still on regex! i want learn it but i'm still crashing the head into my key开发者_开发问答bord! ;-)

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消