1

I use the following function to clean-up my string before I use it on my export:

function cleanString($source) {
    $source = str_replace('&', 'and', $source);
    $source = preg_replace('~[^\p{L}\p{N}]++~u', ' ', $source);
    return trim($source);
}

This works fine, but I want to modify it to allow , (commas) and parenthesis ( and ).

At the moment, this is what's happening:

Name, Brand Name (Some & Slogan) ==> Name Brand Name Some and Slogan

How can I update the above regex pattern to allow commas and parenthesis?

I tried the following: $source = preg_replace('~[^\p{L}\p{N}]++~u\,\(\)', ' ', $source); and I am getting the error: Unknown modifier '\'.

Any ideas?

2
  • In a character class you do not need to escape parenthesis. Commented Dec 3, 2014 at 12:16
  • @ZoltanMagyar parenthesis are not inside character class. Commented Dec 3, 2014 at 12:17

1 Answer 1

1

You can include (, ) and , in your negation character class:

$source = preg_replace('~[^\p{L}\p{N}(),]+~u', ' ', $source);

RegEx Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Also there is no need to use ++ here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.