I am trying to check for two conditions,
The string should contain a vowel.
The string should contain a space.
Here is what I am writing:
$reg = "/(?=.*(\s)) (?=.*(a|e|i|o|u))/";
But on running:
if ( preg_match($reg,"kka "))
echo "YES.";
else
echo "NO.";
I am getting NO. What am I doing wrong?
((?:.*)[aeiouAEIOU]+(?:.*)[ ]+(?:.*))|(?:.*)[ ]+((?:.*)[aeiouAEIOU]+(?:.*))
You can try with this
Explnation
Here is the correct way to use lookaheads:
^((?=.*\s.*).)((?=.*[aeiou].*).).*$
Demo here:
Regex101
If you want an option which does not involve using a regex would be to remove spaces/vowels from the input string and verify that the resulting length has decreased.
$input = "kka ";
if (strlen(preg_replace("/\s/", "", $input)) < strlen($input) &&
strlen(preg_replace("/[aeiouAEIOU]/", "", $input)) < strlen($input)) {
echo "both conditions satisfied"
else {
echo "both conditions not satisfied"
}
The alternative solution using preg_replace and strpos functions:
$str = " aa k";
if (($replaced = preg_replace("/[^aeiou ]/i", "", $str)) && strlen($replaced) >= 2
&& strpos($replaced, " ") !== false) {
echo 'Yes';
} else {
echo 'No';
}
Related
In trying to figure out how to do it with preg_match, but I'm left with no clue.
So what I'm trying is
$string = 'abc.hello.world.123.hola';
if ($string have 3 or more ".") {
echo 'true';
}
I know you said preg_match, but there is a specific PHP function for this. Use substr_count() :)
$string = 'abc.hello.world.123.hola';
$dots = substr_count($string, '.');
echo $dots; // output: 4
if ($dots >= 3) {
// do something
}
I'm trying to preg_match this string.
$word = "$1$s";
or
$word = "$2$s"
if(preg_match('/^\$[1-9]{1}\$s$/' ,$word)){
echo 'true';
} else {
echo 'false';
}
I tried this but it is not giving a true result.
How can I make this true.
PHP is trying to render the variable $s in the double quoted string. Use single quotes instead and you can remove the {1} inside your regular expression because it is not necessary.
$word = '$1$s';
if (preg_match('/^\$[1-9]\$s$/', $word)) {
echo 'true';
} else {
echo 'false';
}
You can also escape the $ in your double quoted string:
$word = "$1\$s";
// Note $1 doesn't need to be escaped since variables can't start with numbers
Finally, you can see why this wasn't working by seeing what your $word actually equaled (with error reporting enabled):
$word = "$1$s"; // Notice: Undefined variable: s on line #
echo $word; // $1
Try this:
if(preg_match('/\$[1-9]{1}\$s/',$word)){
echo 'true';
} else {
echo 'false';
}
I tried to remove the first letter if it is 'r' for example by using ltrim function, but it didn't work.
How can check if the first letter if the word 'r' or 'n' etc, by using "if"
<?php
$string = "no";
if (ltrim($string ,'r')) {
echo 'Yes';
}
?>
Here is a one liner, everyone else used substring, here is something different:
preg_replace('/(^[Rr])/', '', $string);
Edit
The above will replace it, misunderstood the question. Here you can do the if:
if(preg_match('/(^[Rr])/', $string)){
echo "Yes";
}
String elements can be accessed like it's an array ...
if(strtolower($string[0]) == 'r'){
$string = substr($string, 1);
}
if($str[0] == 'r') {
$str = substr($str, 1);
}
One possible solution:
if(strtolower(substr($string, 0, 1)) === 'r')
{
//do something
}
to remove the first letter:
$str = substr($string, 1);
ltrim is used to remove whitespaces or other predefined character from the left side of a string. You can check string first character using this :
$string = "no";
if($string[0] == 'r'){
echo "yes";
}
See DEMO
Try that:
<?php
$string = "no";
if(substr($string ,0,1) == 'n'){
echo 'Yes';
}
?>
When someone writes:
"Near Tokyo"
I would like to check first if the $search contains "near" and if it does then take the "Tokyo" into a variable $location.
I tried this:
if(strpos($search, 'near') == true){
$search = explode("near ", $location);
echo $location;
exit();
}
did not work, it does not execute the if statement
You have multiple bugs here:
strpos may return 0, which signifies a match but will not compare equal to true
strpos is case-sensitive, which would make your example not work (look into stripos instead)
explode is also case-sensitive
It would probably be easiest to use a regex for this:
$input = "Near Tokyo";
if (preg_match('/near\s+(\w+)/i', $input, $matches)) {
echo "Near: ".$matches[1]."\n";
}
else {
echo "No match.\n";
}
See it in action.
This particular regex will only match the next word after "near", but this can be modified to suit your requirements.
returntype of strpos is int, not bool
http://php.net/manual/en/function.strpos.php
so use (according to manual pages) this:
if(strpos($search, 'near') !== false)
change it to:
if(strpos($search, 'near') !== false){
$search = explode("near ", $location);
echo $location;
exit();
}
just take a look at the documentation where this behaviour is explained:
It is easy to mistake the return values for "character found at
position 0" and "character not found". Here's how to detect the
difference:
<?php
$pos = strrpos($mystring, "b");
if ($pos === false) { // note: three equal signs
// not found...
}
?>
Yes, strpos, cumbersome boolean result handling. You probably should or should want to use stristr instead, which is also case-insensitive:
if (stristr($search, "Near")) {
And since you are extracting text anyway, why not use a regex? (People are using the awful explode workaround way too often.)
if (preg_match("'Near (\S+)'i", $search, $match)) {
echo $match[1];
}
use this to get a result:
if(strpos($search, 'near') !== false){
$location = explode("near ", $search);
print_r($location);
exit();
}
$search = "Near Tokyo";
if(strpos($search, 'Near') === 0){
$location = explode("Near ", $search);
echo $location[1];
exit();
}
<?php
$search = "Near Tokoyo";
if(preg_match("/near ([a-z]+)/i", $search, $match))
{
$location = $match[1];
echo $location;
}
?>
EDIT:
Fixed some bugs in your code.
if(stripos($search, 'near') !== false){
$location = explode("near ", $search);
echo $location[0];
exit();
}
I've wanted to check if a number in PHP was proper binary. So far I've added this to check if it is devisable by 8:
if(strlen($binary) % 8 == 0){
return true;
} else {
return false;
}
It works, but it obviously allows other numbers to be placed in, such as 22229999.
What method can I use to make sure only 1's and 0's are in the string? such as 10001001. Not sure how to go about this.
This may be faster than firing up the regex engine:
if (strspn ( $subject , '01') == strlen($subject)) {
echo 'It\'s binary!';
} else {
echo 'Not binary!';
}
If you just look for simple characters or want to count them, regex is often quite slow while one or two built in string function can do the job much faster.
Maybe this does apply here, maybe not.
After hearing some remarks in the comments, that this might not work, here's a test case:
<?php
$strings = array('10001001', '22229999', '40004000');
foreach ( $strings as $string )
{
if ( strspn( $string, '01') == strlen( $string ) ) {
echo $string . ' is binary!' . "\n";
} else {
echo $string . ' is NOT binary!' . "\n";
}
}
It does the job.
for variety
if (!preg_match('/[^01]/', $str))
{
echo 'is binary';
}
if (preg_match('~^[01]+$~', $str))
{
echo 'is binary';
}
Inspired by Techpriester's answer (fixed bug):
if (in_array(count_chars($str, 3), array('0', '1', '01'))
{
echo 'is binary';
}
if ( preg_match('#^[01]+$#', $string) )