trim lines and shrink whitespaces using regex for multi line string

前端 未结 5 1839
轮回少年
轮回少年 2021-01-22 05:53

I\'m using a php function want to create a function to trim all unnecessary white spaces from a multi line string.

The regex that it\'s not working is the one tha

5条回答
  •  粉色の甜心
    2021-01-22 06:32

    You need to /gm instead of just /m

    The code should become: (this code won't work, the update one will)

    $patterns[] = ['/ +$/mg', ''];
    

    Working example here: https://regex101.com/r/z3pDre/1

    Update:

    The g identifier, don't work like this. We need to replace preg_match with preg_match_all

    Use the regex without g, like this:

    $patterns[] = ['/ +$/m', ''];
    

提交回复
热议问题