Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check for special characters php [duplicate]

Tags:

php

Possible Duplicate:
preg_match php special characters

Hi all, I want to check if these characters exist in a string by using preg_match:

^'£$%^&*()}{@'#~?><>,@|\-=-_+-¬'

Help please!

like image 947
stefanosn Avatar asked Oct 14 '10 22:10

stefanosn


People also ask

How do you check if a string contains a special character in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How to escape HTML tags in PHP?

Definition and Usage. The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped. This cannot be changed with the allow parameter.

How remove all special characters from a string in PHP?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).


2 Answers

<?php  $string = 'foo';  if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string)) {     // one or more of the 'special characters' found in $string } 
like image 78
chigley Avatar answered Sep 23 '22 06:09

chigley


preg_match('/'.preg_quote('^\'£$%^&*()}{@#~?><,@|-=-_+-¬', '/').'/', $string);

like image 23
Petah Avatar answered Sep 25 '22 06:09

Petah