1

I have some titles, for example:

should? be fenêtre!

ﻟﻔﺮﻧﺴﻴﺔ-تعاني!!!

What regex expression can i use to remove special characters like: ?,!,^

I need to get those titles like this:

should-be-fenêtre

ﻟﻔﺮﻧﺴﻴﺔ-تعاني

I tried

$name = preg_replace("~[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+~", "-", $name);

But i get

Warning: preg_replace(): No ending delimiter '~' found in

Thanks

2
  • By "special characters" you actually mean punctuation? Commented Feb 4, 2013 at 20:12
  • Yes, all characters like: .,?/*&^%$ etc. Commented Feb 5, 2013 at 11:07

2 Answers 2

3

You can use a couple of regular expressions to strip out anything that is not a letter or a number and condense runs of whitespace and dashes to just a single dash:

// Replaces every non-letter, non-digit with a dash
$str = preg_replace('/(?=\P{Nd})\P{L}/u', '-', $str);

// Replaces runs of whitespace and dashes with a single dash
$str = preg_replace('/[\s-]{2,}/u', '-', $str);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Can you say what \P{Nd} and \P{L} do?
0

Try this:

$text = preg_replace("/(?![.=$'€%-])\p{P}/u", "", $text);

Just change the assertion to match whatever Unicode characters you want to keep.

Comments

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.