We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Regexp remove only special charachters

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

3
Contributors
12
Replies
3 Days
Discussion Span
6 Months Ago
Last Updated
13
Views
Question
Answered
OsaMasw
Junior Poster
161 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,547 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,497
Skill Endorsements: 98
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] => م
        )

)
diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,538
Skill Endorsements: 61

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 )
}
?>
OsaMasw
Junior Poster
161 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,538
Skill Endorsements: 61

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 (# ? % $ £ )

OsaMasw
Junior Poster
161 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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'

diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,538
Skill Endorsements: 61

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

OsaMasw
Junior Poster
161 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

I get this:

Scr4

Attachments Scr4.png 1.56KB
diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,538
Skill Endorsements: 61

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

diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,538
Skill Endorsements: 61

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

OsaMasw
Junior Poster
161 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

OK, I'm on 5.3.5 / Win

diafol
Keep Smiling
Moderator
10,846 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,538
Skill Endorsements: 61

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)

OsaMasw
Junior Poster
161 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 6 Months Ago by diafol and pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0925 seconds using 2.69MB