1

I'm trying to remove all special character like

()[]{}~`@#$%^&*_+=/|.,،;:?؟><

but this code will remove all spec character plus non english character, i want only remove spec character not non-english. i means only accept english+non english but not special character.

preg_replace("/[^A-Za-z0-9\-]/", "-", $_REQUEST["title"]);
Pedram
  • 15,766
  • 10
  • 44
  • 73
  • You can try [here](http://stackoverflow.com/questions/6073221/php-remove-special-character-from-string) – GeekRiky Mar 14 '15 at 09:06
  • There is no such thing as a "special character". There are only characters. And maybe some you are not used to. But that is not exactly a technical definition. So what character range is it you want to remove or keep? – arkascha Mar 14 '15 at 09:08
  • @arkascha well, i want to use it for pretty url, and i want remove at least those are not suitable for SEO and sql injection and etc... like ?!&$% .. – Pedram Mar 14 '15 at 09:11
  • So you are not able to name the characters you want to replace? But you expect an answer? Sorry, I am not trying to be picky here. It is just: without a clear question, how do you expect an answer? – arkascha Mar 14 '15 at 09:17
  • Apart from that: you cannot prevent sql injection by means of that. For that you have to use "prepared statements" when you setup your statements. Everything else does not make any sense whatsoever. What you are trying is like this: well, I want to put out the fire. But I do not dare looking at it. So I cut down a tree. Fire burns wood. So maybe it dies when I cut down that tree. – arkascha Mar 14 '15 at 09:19
  • @arkascha yep, actually we don't know which character will insert in content's title. can't name certain character. just know for URL seo and .. we should use english charachter and non english plus number. a-z A-Z 0-9 + non english character, like arabic/persian and .. character. – Pedram Mar 14 '15 at 09:22
  • @arkascha yes you right, but probably there is a solution for this case, isn't it? – Pedram Mar 14 '15 at 09:26
  • As said: it is impossible to answer a question which does not name what it asks for. Ask yourself: how do you want to replace all characters you want to replace, when you do not know what characters you want to replace? That is logically impossible. – arkascha Mar 14 '15 at 09:27
  • @arkascha, i think some mistake happened here, at least i can list these characters to replace ()[]{}~`@#$%^&?؟*_+=/|.,،;:> – Pedram Mar 14 '15 at 09:30
  • OK, that is a start. Then create a regex matching these characters (you can use one of those online regex testing tools) and feed it into the `preg_replace()` function. – arkascha Mar 14 '15 at 09:32

2 Answers2

1

As a result of the discussion in the comments this might get you started:

<?php

$subject = "This is a string ()[]{}~`@#\$%^&?؟*_+=/|.,،;:' getting stripped.";

$pattern = sprintf('/[%s]/', preg_quote("()[]{}~`@#$%^&?؟*_+=/|.,،;:'", '/'));
$subject = preg_replace($pattern, '', $subject);

echo $subject."\n";

About the sql injection prevention you also mentioned: as said in the comments to the question you have to use a modern database adapter (mysqli or PDO) and "prepared statements". You will find an explanation about that in the documentation. Everything else is "fixing the problem only a bit" which does not make sense at all.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • thanks for answer, it works good. but look like it won't remove DOT in end of the subject line. – Pedram Mar 14 '15 at 10:14
  • Oops, sorry, forgot the square brackets around the character set... I change the answer slightly. – arkascha Mar 14 '15 at 10:20
  • great answer arkascha, and much appreciated for your help. – Pedram Mar 14 '15 at 10:24
  • Glad I could be of help. However do not forget: _this is not a solution to your issue_. There cannot be one until you found out your question. Do not stop with a part of a solution. you will hate yourself for that later, when you run into problems. – arkascha Mar 14 '15 at 10:26
  • yes you right , i know. still .. there are more characters that we didn't mentioned in replace list. – Pedram Mar 14 '15 at 10:35
1

Use unicode property:

preg_replace("/[^\p{L}\p{N}]/u", "-", $_REQUEST["title"]);

This will replace any character that is not a letter and not a digit by a dash.

Edit according to comment:

$regex = array('/[^\p{L}\p{N}\s]/u', '/\s/');
$repl  = array('', '-');
preg_replace($regex, $repl, $_REQUEST["title"]);
Toto
  • 89,455
  • 62
  • 89
  • 125
  • nice idea, but it have a little problem, it works with english character but not working with non english. please see this code $title = "سلام"; $output = preg_replace("/[^\p{L}\p{N}]/", "-", $title); echo $output; in phptester.net/ – Pedram Mar 14 '15 at 11:50
  • @Alipour: Sorry I've forgotten to add the `/u` modifier. See my edit. – Toto Mar 14 '15 at 12:54
  • Thanks, i think this one is better for my case, because it won't let any character except english and non english and numbering and there is no need to define any character to remove. – Pedram Mar 14 '15 at 13:04
  • is there any chance to set, if space convert it to dash, else remove it. ? – Pedram Mar 14 '15 at 13:07