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";
    exit();
    }

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

I think this SO thread may be of use. It appears problematic.

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

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

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


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

echo "<pre>";
print_r($matches);
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:

Array
(
    [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

<?php
$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 )
}
?>
Member Avatar for diafol

OK

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

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

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

if(count($matches[1])){
    $y = array_filter(array_map("trim",$matches[1]));
    if(count($y)){
        echo "Bad characters";
        echo "<pre>";
        print_r($y);
        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 (# ? % $ £ )

Member Avatar for diafol

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'

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

Member Avatar for diafol

I get this:

Scr4

Member Avatar for diafol

Which version of php are you using - did you read pritaeas' link?

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

Member Avatar for diafol

OK, I'm on 5.3.5 / Win

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.