Hello guys, i have problem with regular expression , I have search box, I tried to enter (',#,$,%,^,&,"star",@,!,") or any other special charachters, it give me the whole database items, so i want to limit the enrty to only letters [a-z] numbers [0-9] and arabic charachters [ا-ي]
or to be clear any letter is allowed except for special charachters
I used this but it not allowed english

if(preg_match('/[^\w\d_ -]/si', $search)) {
    echo " You entered not allowed letters";

and if I put !preg_match the arabic will not be allowed, what should I put in regexp ? BTW spaces are allowed

4 Years
Discussion Span
Last Post by OsaMasw
header('Content-Type: text/html; charset=utf-8');

$pattern = "/(\p{Arabic})+/u";

$str = "مرحبا كيف حالك اليوم";

$x = preg_match_all($pattern, $str, $matches);

echo "<pre>";
echo "</pre>";

This seems to work for me wrt arabic characters, but not being an arablic speaker, I don't know if it works - I get this:

    [0] => Array
            [0] => مرحبا
            [1] => كيف
            [2] => حالك
            [3] => اليوم

    [1] => Array
            [0] => ا
            [1] => ف
            [2] => ك
            [3] => م


I think you didn't understand what I need, I need to detect only special charachter, I don't care what user input language the important thing I don't need speciaql characters
I need an expression to detect those characters and show refuse msg

$string = "I dont need specia'l @charachters # " // anything , any language
if (preg_match([//regexp to detect special characters],$string) {
$msg = "not allowed";
else {
// do anything I want ( search - register - mail )

Edited by OsaMasw



header('Content-Type: text/html; charset=utf-8');

$str = "مرحبا 9 8 7 # ? k l % $ £ كيف حالك Alabaster - اليوم";

$x = preg_match_all('/(\W)/u', $str, $matches);

    $y = array_filter(array_map("trim",$matches[1]));
        echo "Bad characters";
        echo "<pre>";
        echo "</pre>";

That should list all your special characters.


thanks for reply, but when tried the code you gave me, it trimmed all Arabic characters, which I need, I need to trim only special charachters (# ? % $ £ )


I need to trim only special charachters (# ? % $ £ )

Sorry, perhaps I'm being dense. OK

header('Content-Type: text/html; charset=utf-8');

$str = "مرحبا 9 8 7 # ? k l % $ £ كيف حالك Alabaster - اليوم";

$x = preg_replace('/([^\d\w ])/u','', $str);

echo $x;

I'ma ssuming you want to keep spaces. Otherwise use '/(\W)/u'

Edited by diafol


I don't know whats wrong but last code not works,
Nothing is echoed
what is the desire output for this ?
must be

مرحبا 9 8 7كيف حالك Alabaster

but it show me nothing if I write only english letters it will show me the english
if I write arabic characters nothing echoed


I read the post by @pritaeas at first I didn't ubderstand , but now I relized
maybe my php version is the problem
My PHP version is 5.2.6, now am downloading the latest version of php and I'll try


Thanks, for reply, I dont know what the wrong, I've installed WAMP and it give me errors not found in appserve, and it ruin my project, I'll back to my version 5.2.6 and keep working untill this problem resolved,SOLVED (Nearly)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.