Home php php: regular expression for preg_split ()

php: regular expression for preg_split ()




I am writing a regular expression for preg_split () so that the string is split at spaces + all possible punctuation marks. While it looks like this:

$ pattern = "/ [\s,;.\!\-\?\:\(\)>+/ ";
  1. How to add an em dash, all possible quotes, etc. to an expression. (maybe you forgot any other punctuation marks)?

  2. preg_split () with this expression adds an empty element to the end of the array. It is clear that it can be cut out later, but I do not like this solution. How can I correct the expression so that there is no empty element?

Thank you!

Answer 1, authority 100%

    1. It is necessary to decide what exact result is needed. If only punctuation marks, then you have to completely enumerate all desired characters in the character class, including em dash, all possible quotes, etc. – there is no generic character class to describe this.
    2. If you want to achieve a result that divides by all literals that are not spaces and letters, that is, the character class [: punct:] . In a regular expression, it will look like this:

      / [\ s [: punct:]] + /
  1. To exclude empty elements, just pass the PREG_SPLIT_NO_EMPTY flag to the function preg_split

    preg_split ("/ [\ s [: punct:]] + /", $ text, -1, PREG_SPLIT_NO_EMPTY)


Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions