Im using the following regex to check if a string contains any of the following words:
/(work|hello|yes)/
How could I reverse it, to check if the string instead does not contain any of the following words?
if (preg_match('/(work|hello|yes)/', trim(strtolower($mystring)))) {
}
Note I dont want to use !preg_match
Could use a negative looakhead to match the string, if it does not contain the words:
^(?!.*?(?:work|hello|yes)).*
Also might want to add \b word boundaries, before/after the word.
Test at regex101.com
If it's multiline input, use with s (PCRE_DOTALL) flag to make the . also match newlines.
Try this :
$match = '/work|hello|yes/';
$myString = trim(strtolower($mystring));
if (preg_match($match,$myString)) {
}
You missed ' first parameter of preg_match
$mystring='hello world';
if (preg_match('/(work|hello|yes)/', trim(strtolower($mystring)))) {
echo 'Yes'; //Result is yes
}else{
echo 'No';
}
Related
According to PHP manual "If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on."
How can I return a value from a string with only knowing the first few characters?
The string is dynamic and will always change whats inside, but the first four character will always be the same.
For example how could I return "Car" from this string "TmpsCar". The string will always have "Tmps" followed by something else.
From what I understand I can return using something like this
preg_match('/(Tmps+)/', $fieldName, $matches);
echo($matches[1]);
Should return "Car".
Your regex is flawed. Use this:
preg_match('/^Tmps(.+)$/', $fieldName, $matches);
echo($matches[1]);
$matches = []; // Initialize the matches array first
if (preg_match('/^Tmps(.+)/', $fieldName, $matches)) {
// if the regex matched the input string, echo the first captured group
echo($matches[1]);
}
Note that this task could easily be accomplished without regex at all (with better performance): See startsWith() and endsWith() functions in PHP.
"The string will always have "Tmps" followed by something else."
You don't need a regular expression, in that case.
$result = substr($fieldName, 4);
If the first four characters are always the same, just take the portion of the string after that.
An alternative way is using the explode function
$fieldName= "TmpsCar";
$matches = explode("Tmps", $fieldName);
if(isset($matches[1])){
echo $matches[1]; // return "Car"
}
Given that the text you are looking in, contains more than just a string, starting with Tmps, you might look for the \w+ pattern, which matches any "word" char.
This would result in such an regular expression:
/Tmps(\w+)/
and altogether in php
$text = "This TmpsCars is a test";
if (preg_match('/Tmps(\w+)/', $text, $m)) {
echo "Found:" . $m[1]; // this would return Cars
}
I need to check to see if a variable contains anything OTHER than 0-9 and the "-" and the "+" character and the " "(space).
The preg_match I have written does not work. Any help would be appreciated.
<?php
$var="+91 9766554433";
if(preg_match('/[0-9 +\-]/i', $var))
echo $var;
?>
You have to add a * as a quantifier to the whole character class and add anchors to the start and end of the regex: ^ and $ means to match only lines containing nothing but the inner regex from from start to end of line. Also, the i modifier is unnecessary since there is no need for case-insensitivity in this regex.
This should do the work.
if(!preg_match('/^[0-9 +-]*$/', $var)){
//variable contains char not allowed
}else{
//variable only contains allowed chars
}
Just negate the character class:
if ( preg_match('/[^0-9 +-]/', $var) )
echo $var;
or add anchors and quantifier:
if ( preg_match('/^[0-9 +-]+$/', $var) )
echo $var;
The case insensitive modifier is not mandatory in your case.
You can try regex101.com to test your regex to match your criteria and then on the left panel, you'll find code generator, which will generate code for PHP, Python, and Javascript.
$re = "/^[\\d\\s\\+\\-]+$/i";
$str = "+91 9766554433";
preg_match($re, $str, $matches);
You can take a look here.
Try see if this works. I haven't gotten around to test it beforehand, so I apologize if it doesn't work.
if(!preg_match('/^[0-9]+.-.+." ".*$/', $var)){
//variable contains char not allowed
}else{
//variable only contains allowed chars
}
I have variable like this
$string = "Hello World";
I want to compare its with properly format:
$formatstring = 'anystringornumber/anystringornumber/anystringornumber/anystringornumber/number';
This is my PHP usage:
$key = "Kode Parkir 1/01012015/Shift1/Suhendra/25000";
$regex = '^[A-Za-z]/[A-Za-z]/[A-Za-z]/[A-Za-z]/[0-9]^';
if (preg_match($regex, $key)) {
echo 'Passed';
} else {
echo 'Wrong key';
}
The result always Wrong Key.
Your regex is incorrect instead use
$regex = '~[a-z\d]+/[a-z\d]+/[a-z\d]+/[a-z\d]+/[\d]+~i';
Demo
You want to match alphanumeric character (letter and number), but didn't add the numbers in the regex. Also you missed + to match multiple characters. Secondly don't use ^ for enclosing the pattern. It is used as a special character in regex, which means start of string. You can use # instead. Like this:
$regex = '#[A-Za-z0-9]+/[A-Za-z0-9]+/[A-Za-z0-9]+/[A-Za-z0-9]+/[0-9]+#';
But if you want to use ^ and $ with their special meaning it will be like this :
$regex = '#^[A-Za-z0-9]+/[A-Za-z0-9]+/[A-Za-z0-9]+/[A-Za-z0-9]+/[0-9]+$#';
A recent problem I've encountered
$string = 'Demetria Devonne';
if (strpos($string, 'Devon'))
{
echo 'Devon';
}
Returns a result. But I need to only find matches for the specified word where a letter does not come before or after.
So running a check on words like
"Devons" or "Devonia"
would not result in a match.
But then something like
"From Devon, Uk"
would result in a match as there are no letters surrounding it.
Is there a function that can do this in PHP?
You will need to do this using regex, rather than string functions. Fortunately, this is very simple in this case.
if (preg_match('/\bDevon\b/', $string)) {
echo 'Devon';
}
\b is a special character that means "word boundary", such as a comma or a space.
use str_word_count() instead. u get array with words, and then u can use in_array() to check if ur word is there :s
Maybe using strstr() : http://php.net/manual/en/function.strstr.php
$email = 'name#example.com';
$domain = strstr($email, '#');
echo $domain; // prints #example.com
Mixing it with substr : substr(strstr($string, 'Devon'),0,5) could do the job
I am trying to verify in PHP with preg_match that an input string contains only "a-z, A-Z, -, _ ,0-9" characters. If it contains just these, then validate.
I tried to search on google but I could not find anything usefull.
Can anybody help?
Thank you !
Use the pattern '/^[A-Za-z0-9_-]*$/', if an empty string is also valid. Otherwise '/^[A-Za-z0-9_-]+$/'
So:
$yourString = "blahblah";
if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) {
#your string is good
}
Also, note that you want to put a '-' last in the character class as part of the character class, that way it is read as a literal '-' and not the dash between two characters such as the hyphen between A-Z.
$data = 'abc123-_';
echo preg_match('/^[\w|\-]+$/', $data); //match and output 1
$data = 'abc..';
echo preg_match('/^[\w|\-]+$/', $data); //not match and output 0
You can use preg_replace($pattern, $replacement, $subject):
if (preg_replace('/[A-Za-z0-9\-\_]/', '', $string)) {
echo "Detect non valid character inside the string";
}
The idea is to remove any valid chars, if the result is NOT empty do the code.