Checking if a character is in a word in PHP - php

In my program basically your only allowed to use words that contain the letters "IOSHZXN"
I'm trying to figure out a way where you can mix up the letters and it will recognize that it matches.
For example, word SHINT does not match since it has a T, but the word SHINX matches because it contains only the a combination of the letters listed (IOSHZXN)
<?php
$word = "IOSHZNX";
$charactersallowed = "IOSHZXN";
if (preg_match('/IOSHZXN/', $word)) {
echo "YES";
} else {
echo "NO";
}
?>
Any help would be appreciated..

You should use:
if (preg_match('/^[IOSHZXN]+$/', $word)) {
^ and $ make sure the string only the a combination of letters IOSHZXN.

You can do this:
It matches anything that is not one of those letters and returns the opposite:
if (!preg_match('/[^IOSHZXN]+/', $word)) {
echo "YES";
}
Also, if you want it to be case-insensitive, you can use:
if (!preg_match('/[^IOSHZXN]+/i', $word)) {
echo "YES";
}
The [^...] matches anything that is not defined within the brackets.
The + continues to search through the entire string.
The i makes it not care about if letters are capitalized or not.

Related

How to change specific first Letter in string to Capital using PHP?

If the first character of my string contains any of the following letters, then I would like to change the first letter to Uppercase: (a,b,c,d,f,g,h,j,k,l,m,n,o,p,q,r,s,t,v,w,y,z) but not (e,i,u,x).
For example,
luke would become Luke
egg would stay the same as egg
dragon would become Dragon
I am trying to acheive this with PHP, here's what I have so far:
<?php if($str("t","t"))
echo ucfirst($str);
else
echo "False";
?>
My code is simply wrong and it doesn't work and I would be really grateful for some help.
Without regex:
function ucfirstWithCond($str){
$exclude = array('e','i','u','x');
if(!in_array(substr($str, 0, 1), $exclude)){
return ucfirst($str);
}
return $str;
}
$test = "egg";
var_dump(ucfirstWithCond($test)); //egg
$test = "luke";
var_dump(ucfirstWithCond($test)); //Luke
Demo:
http://sandbox.onlinephpfunctions.com/code/c87c6cbf8c616dd76fe69b8f081a1fbf61cf2148
You may use
$str = preg_replace_callback('~^(?![eiux])[a-z]~', function($m) {
return ucfirst($m[0]);
}, $str);
See the PHP demo
The ^(?![eiux])[a-z] regex matches any lowercase ASCII char at the start of the string but e, u, i and x and the letter matched is turned to upper inside the callback function to preg_replace_callback.
If you plan to process each word in a string you need to replace ^ with \b, or - to support hyphenated words - with \b(?<!-) or even with (?<!\S) (to require a space or start of string before the word).
If the first character could be other than a letter then check with an array range from a-z that excludes e,i,u,x:
if(in_array($str[0], array_diff(range('a','z'), ['e','i','u','x']))) {
$str[0] = ucfirst($str[0]);
}
Probably simpler to just check for the excluded characters:
if(!in_array($str[0], ['e','i','u','x'])) {
$str[0] = ucfirst($str[0]);
}

Compare username to regular expression with PHP

I've never used regular expressions before and did some research on how to allow my username field only alphanumeric characters, dashes, dots, and underscores. I have the following expression but it doesn't seem to be working.
$string = "Joe_Scotto";
if (!preg_match('[a-zA-Z0-9_-.]', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
I want the statement to return true if it is following the "guidelines" and false if the username contains something other than what I specified it should contain. Any help would be great. Thanks!
Try this
$string = "Joe_Scotto";
if (!preg_match('/^[A-Za-z0-9_.]+$/', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
You match only a single character. Try this:
$string = "Joe_Scotto";
if (!preg_match('/^[a-zA-Z0-9_.-]+$/', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
The + sign says: match 1 or more characters defined directly before the + (* is the same but matches 0 or more characters).
Also the separators '/' (or any other separator characters) are required.
And in character classes, it is better to place the - sign to the end, else it could be misinterpreted as range from _ to .
And add ^ at the beginning (this means: match from the beginning of the input) and $ to the end (this means: match to the end of the input). Else, also a part of the string would match.
You should use something like that http://www.phpliveregex.com/p/ern
$string = 'John_Buss';
if (preg_match('/[A-z0-9_\-.]+/', $string)) {
return true;
} else {
return false;
}
Make sure to add / delimiter character at the start and the end of your regex
Make sure to use \ escape character before -
Make sure to add + character quantifier

preg_match issues using php variable

I have a variable I want to use in a preg_match combined with some regex:
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-/d.*/", $string)) {
echo "matched";
}
In my pattern I am trying to match using cheese, followed by a - and 1 digit, followed by anything else.
change /d to \d
there is no need to use .*
if your string is defined by user (or may contains some characters (e.g: / or * or ...)) this may cause problem on your match.
Code:
<?php
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-\d/", $string))
{
echo "matched";
}
?>
You mistyped / for \:
if(preg_match("/$find-\d.*/", $string)) {
The .* is also not really necessary since the pattern will match either way.
for digit, it's \d
if(preg_match("/$find-\d.*/", $string)) {

What's a regular expression for a certain ruleset in PHP?

Specifically, it should be 6 or more alphanumerics (0-9 + a-z).
The second character is a letter.
The third character is an odd number.
Any help?
An example regex that matches this for ASCII is
^[0-9A-Za-z][A-Za-z][13579][0-9A-Za-z]{3,}$
PHP code
<?php
$test = '0A1000';
if (preg_match('/^[0-9A-Za-z][A-Za-z][13579][0-9A-Za-z]{3,}$/', $test)) {
// Do some stuff
echo "matched";
}

PHP preg_match for only numbers and letters, no special characters

I don't want preg_match_all ... because the form field only allows for numbers and letters... just wondering what the right syntax is...
Nothing fancy ... just need to know the right syntax for a preg_match statement that looks for only numbers and letters. Something like
preg_match('/^([^.]+)\.([^.]+)\.com$/', $unit)
But that doesn't look for numbers too....
If you just want to ensure a string contains only alphanumeric characters. A-Z, a-z, 0-9 you don't need to use regular expressions.
Use ctype_alnum()
Example from the documentation:
<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo "The string $testcase consists of all letters or digits.\n";
} else {
echo "The string $testcase does not consist of all letters or digits.\n";
}
}
?>
The above example will output:
The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
if(preg_match("/[A-Za-z0-9]+/", $content) == TRUE){
} else {
}
If you want to match more than 1, then you'll need to, however, provide us with some code and we can help better.
although, in the meantime:
preg_match("/([a-zA-Z0-9])/", $formContent, $result);
print_r($result);
:)

Categories