How to check if a string contains an exact substring in Php - php

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";
}

Related

PHP issue with IF statement

I am trying to check for a word in every created sting.
So I am using "if" after "if" in order to check the word in every sting but also if the word is there to print out every thing in which find it.
But i want if the searched word is not found in ANY of the strings to go to else statement No such word.
But now the code posted "no such word" even if Only ONE of the "IFs" is not ok.
Here is the code:
if(stripos($str, $text)){
/*echo $str ;*/ echo $str = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str);
echo "</br>";
}
if(stripos($str2, $text)){
/*echo $str2 ;*/ echo $str2 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str2);
}
if(stripos($str3, $text)){
/*echo $str3 ;*/ echo $str3 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str3);
}
if(stripos($str4, $text)){
/*echo $str4 ;*/ echo $str4 = preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$str4);
}
else
{
echo "No such word"; *** Now will print it if cannot find the $text in ANY of the $str -ings
}
$str,$str1,$str2... are the strings. $text-> is the variable for search.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
I tried to put If statement in IF but didn't work.
Please tell me how to have all the strings checked and displayed if the searched word is there, and if in none of the is found Only Then to have else executed.
Thank you.
Your else statement from your code example only applies to the last if:
$arr_string = [
'string1',
'string2',
'string3',
'string4',
];
$found = false;
foreach ($arr_string as $value) {
if(stripos($value, $text) !== false) {
echo preg_replace("/\b([a-z]*${fwcode}[a-z]*)\b/i","<font color ='red'><b>$1</b></font>",$value);
$found = true;
}
}
if (!$found) {
echo "No such word";
}
This way you can easily check as many strings as you want. Using a loop like foreach or for.
Also read about the use of stripos: https://www.php.net/manual/en/function.stripos.php
This function may return Boolean false, but may also return a
non-Boolean value which evaluates to false.

php - echo not showing without other echo

I got this error and I don't really know why.
switch($op)
{
case "check" :
$result = $Login->login($data,array());
break;
}
echo json_encode($result);
and, it's not echoing the string.
if I put an echo before or after, it works
echo json_encode($result);
echo '.';
returns
{"status":false,"errID":4,"errTxt":"Invalid password"}.
var_dump returns
string(50ish) "{"status":false,"errID":4,"errTxt":"Invalid password"}"
Any ideeas why it's not echoing it without other stuff echoed too?
Edit
$str = json_encode($result);
echo $str;
returns nothing but
$str = json_encode($result);
$str .= '-';
echo $str;
returns
{"status":false,"errID":3,"errTxt":"Invalid email"}-
So it won't echo the string, but if I append something after it, it will echo it.
LE: with die() after echo sill nothing

Repeat a php code - how to?

I want this code to repeat for the given id ( if it doesn't find for id shown in example so it will go for 1232 (+1) and so on untill it finds a good id that has the preg_matsh "flv_url" and stops showing me the id that worked)
<?php
$id=1231;
$text = file_get_contents("http://www.exemple.com/video". $id ."");
if (preg_match('~flv_url=(.*?)&~si', $text, $body)){
$decoded_url = rawurldecode($body[1]);
echo $decoded_url;
}
else {}
?>
<?php
$id=1231;
$match = false;
do {
$text = file_get_contents("http://www.exemple.com/video". $id++ ."");
if (preg_match('~flv_url=(.*?)&~si', $text, $body)){
$match = true;
$decoded_url = rawurldecode($body[1]);
echo $decoded_url;
}
} while(!$match);
?>
you need to use while() or for() or do() for repeat your code.code will repeat according to your conditions
You can use for();
<?php
for($id=1231;$id!=-1;$id++){
$text = file_get_contents("http://www.exemple.com/video". $id ."");
if (preg_match('~flv_url=(.*?)&~si', $text, $body)){
$decoded_url = rawurldecode($body[1]);
echo $decoded_url;
$id = -1;
}
else {}
}
?>

How to put URL in array and search through array for matching string?

I am trying to search through a URL for a matching string, but the below code snippet doesn't seem to work.
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file = file($url);
if (in_array($search,$file)) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>
If you are just searching for an occurrence of a string on the page, you can use
$str = file_get_contents($url);
if (strpos($str, $search) !== false) {
echo 'Success!';
} else {
echo 'Fail';
}
in_array() checks if an array member is equal to your needle.
It is improbable many websites will have a line which is equal to a only.
Also, is allow_url_fopen enabled?
That code will only find a line that has the exact $search string (likely including whitespace). If you're parsing HTML, check PHP's DOMDocument classes. Or, you can use a regex to pull what you need.
As #alex says, check is allow_url_fopen is enabled.
Also you can use strpos to search the string:
<?php
$url = "http://www.drudgereport.com";
$search = "a";
$file_content = file_get_contents($url);
if (strpos($file_content, $search) !== false) {
echo "Success!";
} else {
echo "Can't find word.";
}
?>

Error in preg_match PHP

<?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.

Categories