Replacing dumb quotes for smart quotes with preg_replace

前端 未结 1 879
野性不改
野性不改 2021-01-27 05:43

I\'m trying to swap dumb quotes for their smart equivalent. So I want to turn Jane said \'How do we do this?\' \'I don\'t know\' replied Sam. into Jane said ‘

1条回答
  •  醉话见心
    2021-01-27 06:23

    $singlequotesPattern = "/'(.*?)'/";
    

    Add a ? to make the * quantifier non-greedy. A greedy quantifier finds the longest match possible. A non-greedy one finds the shortest.

    Greedy:

    Jane said 'How do we do this?' 'I don’t know' replied Sam.
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    Non-greedy:

    Jane said 'How do we do this?' 'I don’t know' replied Sam.
               ^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^
    

    0 讨论(0)
提交回复
热议问题