<?php
$string = 'foo';
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string))
{
// one or more of the 'special characters' found in $string
}
Answer from chigley on Stack OverflowHow about using a regex:
if (preg_match('/[^a-zA-Z]+/', $your_string, $matches))
{
echo 'Oops some number or symbol encountered !!';
}
else
{
// Everything fine... carry on
}
If you just want to check whether the string $input consists only of characters a-z and A-Z you can use the following:
if(!preg_match('/^\[a-zA-Z]+
input)) {
// String contains not allowed characters ...
}
Assuming that you mean html entities when you say "special chars", you can use this:
<?php
$table = get_html_translation_table(HTML_ENTITIES, ENT_COMPAT, 'UTF-8');
$chars = implode('', array_keys($table));
if (preg_match("/[{$chars}]+/", $string) === 1) {
// special chars in string
}
get_html_translation_table gets all the possible html entities. If you only want the entities that the function htmlspecialchars converts, then you can pass HTML_SPECIALCHARS instead of HTML_ENTITIES. The return value of get_html_translation_table is an array of (html entity, escaped entity) pairs.
Next, we want to put all the html entities in a regular expression like [&"']+, which will match any substring containing one of the characters inside square brackets of length 1 or more. So we use array_keys to get the keys of the translation table (the unencoded html entities), and implode them together into a single string.
Then we put them into the regular expression and use preg_match to see if the string contains any of those characters. You can read more about regular expression syntax at the PHP docs.
$special_chars = // all the special characters you want to check for
$string = // the string you want to check for
if (preg_match('/'.$special_chars.'/', $string))
{
// special characters exist in the string.
}
Check the manual of preg_match for more details
You can do that with a regex :
<?php
$string = 'classone insertclass_182 classtwo';
$regex = '/insertclass_([0-9]*)/';
$result = preg_match($regex, $string, $matches);
var_dump($matches);
It will return :
array(2) {
[0] =>
string(15) "insertclass_182"
[1] =>
string(3) "182"
}
I think this code will helpful
<?php
$string="insertclass_4";
$pos=strpos($string, 'insertclass_');
if($pos!==false){
$var=substr($string, strlen('insertclass_'));
echo $var;
}
A concise way to do it with your two data structures would be:
count( array_intersect( str_split($my_string), $special_chars ) )
That would also tell you how many of the special characters are in the string.
You could otherwise write a loop for your character list and manually probe with strpos.
The least effort would be converting your special character list into a regex charclass and testing against the string.
If you're just trying to match all non word characters, preg_match_all is probably a better solution. Give it a try.
preg_match_all('/[\W]{1}/',$my_string, $matches);
the \W matches any non-word character and the {1} specified to grab only 1 of them and quit, using preg_match_all instead of preg_match gets all sections that match the regex instead of just the first one.
Now the variable $matches is an array containing all of the non-word characters. If you want to know how many you can do
$numSpecialCharacters = preg_match_all('/[\W]{1}/',$my_string);
If you don't care how many, and just want to check if it contains one, you can just use a conditional
if($numSpecialCharacters === false)
//something went wrong.
elseif( $numSpecialCharacters > 0)
//the string contains special characters
You can find the documentations here.Hope that helps.
No need for all that. Just use one bracket group, negate it (be prepending a ^), and use the return value directly:
function is_clean ($string) {
return ! preg_match("/[^a-z\d_-]/i", $string);
}
Here's a quote from the PHP docs:
Return Values
preg_match()returns1if the pattern matches given subject,0if it does not, orFALSEif an error occurred.
In the regex above, we're looking for any characters in the string that are not in the bracket group. If none are found, preg_match will return 0 (which when negated will result in true). If any of those characters are found, 1 will be returned and negated to false.
Just an other method without regex.
function is_clean ($string) {
{
return ctype_alnum(str_replace(array('-', '_'), '', $input);
}
Maybe I find the time later this day to compare the performance, but I guess 'efficient' in your question was related to the code not the execution time? Letharion did the work for me :)