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';
}
?>
Related
I am trying to check if a number say 1 is present in a string say 11,12,13 To do this I am using strpos:
<?
$s = "2,3,11,12,13";
$ss = "1";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
Here I am expecting the code to give fail as output but it gives success.
Is there any function I can use to exactly match the number in a string. I want the number 1 in the string, not the numbers containing 1 like 11,123,100 etc.
Edit 1: It works fine for #Twinfriends answer but the problem is doing it with multiple numbers.
<?
$s = "2,3,11,12,13";
$ss = "1,2";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
This code gives output fail but it should give true.
Edit 2: Check answer https://stackoverflow.com/a/48297002/8490014
The problem has been solved but because of for each loop the output is getting repeated as many times as there are values in the array. How can we get the output repeating the output?
You could do it like this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$toTest = explode(",", $s);
if(in_array("$ss", $toTest)) {
echo "success";
}
else {
echo "fail";
}
?>
If you've any question considering this code, feel free to ask. (Code tested, works well)
The problem has been solved by adding a foreach loop:
<?
$s = "2,3,11,12,13";
$ss = "1,2,3,5";
$catid = explode(",", $s);
$ocat = explode(",",$ss);
foreach($ocat as $abc) {
if(in_array($abc, $catid)) {
echo "success";
}
else {
echo "fail";
}
}
?>
Try this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$haystack = explode(",",$s);
$matches = preg_grep ("/$ss/", $haystack);
echo !empty($matches) ? "success" : "fail";
You can check by appending comma after the variable to check occurrence at start, appending comma before the variable to check occurrence at last or appending comma before and after the variable to check occurrence at middle.
if((substr($s, 0, (strlen($ss)+1)) === $ss.',')||(substr($s, (strlen($s)-strlen($ss)-1), strlen($s)) === ','.$ss)||(strpos($s, ','.$ss.','))){
echo "success";
}
else{
echo "fail";
}
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';
}
Say I have string such as below:
"b<a=2<sup>2</sup>"
Actually its a formula. I need to display this formula on webpage but after b string is hiding because its considered as broken anchor tag. I tried with htmlspecialchars method but it returns complete string as plain text. I am trying with some regex but I can get only text between some tags.
UPDATE:
This seems to work with this formula:
"(c<a) = (b<a) = 2<sup>2</sup>"
And even with this formula:
"b<a=2<sup>2</sup>"
HERE'S THE MAGIC:
<?php
$_string = "b<a=2<sup>2</sup>";
$string = "(c<a) = (b<a) = 2<sup>2</sup>";
$open_sup = strpos($string,"<sup>");
$close_sup = strpos($string,"</sup>");
$chars_array = str_split($string);
foreach($chars_array as $index => $char)
{
if($index != $open_sup && $index != $close_sup)
{
if($char == "<")
{
echo "<";
}
else{
echo $char;
}
}
else{
echo $char;
}
}
OLD SOLUTION (DOESN'T WORK)
Maybe this can help:
I've tried to backslash chars, but it doesn't work as expected.
Then i've tried this one:
<?php
$string = "b<a=2<sup>2</sup>";
echo $string;
?>
Using < html entity it seems to work if i understood your problem...
Let me know
Probably you can give spaces such as :
b < a = 2<sup>2</sup>
It does not disappear the tag and looks much more understanding....
You could try this regex approach, which should skip elements.
$regex = '/<(.*?)\h*.*>.+<\/\1>(*SKIP)(*FAIL)|(<|>)/';
$string = 'b<a=2<sup>2</sup>';
$string = preg_replace_callback($regex, function($match) {
return htmlentities($match[2]);
}, $string);
echo $string;
Output:
b<a=2<sup>2</sup>
PHP Demo: https://eval.in/507605
Regex101: https://regex101.com/r/kD0iM0/1
I want to get the special chars found in string using PHP code, Here is my trial code:
$page_num =">256";
if(preg_match('^[-<>]+$', $page_num))
{
//Here it should returns special chars present in string..
}
check this
$page_num =">256";
if(preg_match_all('/\W/', $page_num,$matches))
{
print_r($matches);
}
What you have missed is ending delimiter
$page_num =">256";
if(preg_match('^[-<>]+$^', $page_num))
^// this one
{
//Here it should returns special chars present in string..
}
Demo of your code, here
or you can try this,
$string = 'your string here';
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $string))
{
echo "yes";
} else {
echo "no";
}
One more solution is
preg_match('![^a-z0-9]!i', $string);
<?php
$str = "asd,ad";
if(preg_match(",",$str)) {
echo "ok";
}
?
It outputs me
No ending delimiter ',' found in....
?>
your pattern can be replaced to strpos instead
if(strpos($str, ",")!==false)
{
echo "ok";
}
You are missing delimitters, try this:
$str = "asd,ad";
if(preg_match("/,/",$str)) {
echo "ok";
}
To find out more instances, you may want to use preg_match_all function too.