I'm practicing my beginner php skills and would like to know why this script always returns FALSE?
What am i doing wrong?
$namefields = '/[a-zA-Z\s]/';
$value = 'john';
if (!filter_var($value,FILTER_VALIDATE_REGEXP,$namefields)){
$message = 'wrong';
echo $message;
}else{
$message = 'correct';
echo $message;
}
The regexp should be in an options array.
$string = "Match this string";
var_dump(
filter_var(
$string,
FILTER_VALIDATE_REGEXP,
array(
"options" => array("regexp"=>"/^M(.*)/")
)
)
); // <-- look here
Also, the
$namefields = '/[a-zA-Z\s]/';
should be rather
$namefields = '/[a-zA-Z\s]*/'; // alpha, space or empty string
or
$namefields = '/[a-zA-Z\s]+/'; // alpha or spaces, at least 1 char
because with the first version I think you match only single-character strings
Related
I just want to build a php function that checking if string is contain brackets and return boolean, anyone can help?
example:
$string = "{hallo}"; // true
$string2 = "h{allo}"; //true
$string3 = "hal}{o"; //false
$string4 = "hallo{}";//false
Check this
<?php
$string = "{hallo}";
$res = preg_match('/{\w+}/',$string);
print_r($res);
?>
I have a sample code to check if the string subdomain is "us"
$string = "www.domain.com"; // 1
$string = "domain.com"; // 2
$string = "us.domain.com"; // 3
$string = "www.us.domain.com"; // 4
if (preg_match("/^(?=.{2})[a-z0-9]+(?:-[a-z0-9]+)*$/i", $_SERVER['SERVER_NAME'])) {
echo "$string include us";
} else {
echo "$string not include us";
}
But result error, how to fix it
If by "subdomain" you mean that the tld and the main domain are excluded, that means we'll have to check for the text "us" and two dots further in the text.
It will also match with sub-subdomains (Like us.subdomain.domain.us)
<?php
$domains = [
'www.domain.us',
'domain.us',
'domainuscomputers.com',
'us.domain.us',
'www.asuscomputers.usdomain.us',
'us.subdomain.domain.us',
];
$reg = '/us.*\..*\./';
foreach ($domains as $domain) {
$match = preg_match($reg, $domain) ? 'subdomain containing "us" found' : 'No match';
printf('domain %s : %s<br>', $domain, $match);
}
Note that the regular expression may be improved, but hey, it works !
In this specific case I don't see the need of using regular expressions.
I think a simple strpos will do the job.
Try this code:
$string = "www.domain.com"; // 1
$string = "domain.com"; // 2
$string = "us.domain.com"; // 3
$string = "www.us.domain.com"; // 4
if (strpos($string,'us.')) {
echo "$string include us";
} else {
echo "$string not include us";
}
In php, how can I check if a string has no characters at all.
Currently I do like below, and replace - with ' '. But if a search string contained all bad words, it'll leave me with ' '(3 blank spaces). The length will still show as 3 and it'll head off to the sql processor. Any method to check if a string has no characters or numbers at all?
$fetch = false;
#$strFromSearchBox = 'Why-you-foo-bar-I-ought-to-tar-you';
$strFromSearchBox = 'foo-bar-tar';
if(strlen($strFromSearchBox) >=2)
{
$newString = str_replace($theseWords,'',$strFromSearchBox);
$newString = str_replace('-',' ',$newString);
if(strlen($newString)>=2)
{
$fetch = true;
echo $newString;
}
}
if($fetch){echo 'True';}else{echo 'False';}
$fetch = false;
#$strFromSearchBox = 'Why-you-foo-bar-I-ought-to-tar-you';
$strFromSearchBox = 'foo-bar-tar';
if(strlen($strFromSearchBox) >=2)
{
$newString = str_replace($theseWords,'',$strFromSearchBox);
$newString = str_replace('-',' ',$newString);
$newString=trim($newString); //This will make the string 0 length if all are spaces
if(strlen($newString)>=2)
{
$fetch = true;
echo $newString;
}
}
if($fetch){echo 'True';}else{echo 'False';}
If you strip the leading and backmost spaces, the length will go down to 0 which you can easily turn into the $fetch boolean:
$fetch = (bool) strlen(trim($newString));
See trimDocs.
Use regex perhaps...
if (preg_match('/[^A-Za-z0-9]+/', $strFromSearchBox))
{
//is true that $strFromSearchBox contains letters and/or numbers
}
I am trying to trim a string in PHP so that I can only get certain text from the String.
I have an email stored to a String for instance some_name#somedomain.com .
How can I remove the text after the '#' so that I would only 'some_name'?
In PHP you can do :
$string = 'some_name#somedomain.com';
$res = explode('#', $string);
echo $res[0];
Or you can use regexp, string functions in php ... etc
You should know both ways to do this:
substr
$mail = "some_name#somedomain.com";
echo substr($mail, 0, strpos($mail, '#') );
explode
list($name, $domain) = explode('#', $mail);
echo $name;
If you don't need the $domain you can skip it:
list($name) = explode('#', $mail);
More about list.
Demo: http://ideone.com/lbvQF
$str = 'some_name#somedomain.com';
$strpos = strpos($str, "#");
echo $email = substr($str, 0,$strpos);
you can try this to get string before #
Try This
$str1 = "Hello World";
echo trim($str1,"World");
You could try split using regex and the # symbol. This will return two Strings which you can then use just to acquire the 'some_name'.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
String s = "some_name#somedomain.com";
String name = s.substring(0,s.indexOf("#");
I want to sanitize the email a#$%#$#b##$#$2344324.com to a#b.com .
I tried and failed
echo filter_var("a#$%#$#b##$#$2344324.com", FILTER_SANITIZE_EMAIL); //result: a#$%#$#b##$#$2344324.com
I need to trim special characters in a email(sanitize to remove special characters). I used below code but I was unsuccessful.
$string = preg_replace("/^[a-zA-Z0-9._%+-]+#(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/", "", "a#$%#$#b##$#$2344324.com");
echo $string;//result: a#b.com -- unwanted characters trimmed here.
There is already a RFC-based solution here: http://fightingforalostcause.net/misc/2006/compare-email-regex.php
function is_valid_email_address($email){
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain";
return preg_match("!^$addr_spec$!", $email) ? true : false;
}