Checking if a string contains " or ' [duplicate] - php

This question already has answers here:
Simple PHP strpos function not working, why?
(5 answers)
Closed 3 years ago.
I am trying to do a input check and to seeif the string contains " or '. I have the stringcheck method that looks like this
public static function stringcheck($search, $string)
{
$position = strpos($string, $search);
if ($position == true)
{
return true;
}
else{
return false;
}
}
And this is how i am trying to check it
if(H::stringcheck($search8, $string) === true || $string[0] === """)
{
$value++;
}
And the $search8 look like $search8 = htmlspecialchars('"');. I should also mention that the $string is already sanitized.

strpos returns the position of the match, which will be 0 if the match is at the beginning. But 0 is not == true.
I guess you are trying to work around that by extra checking $string[0] === """ which won't work, as I commented above: $string[0] is a single character, but " is 6 characters long.

Related

PHP strpos with special characters [duplicate]

This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
If string contains forward slash
(4 answers)
Closed 5 years ago.
i try to check a string with strpos().
$a = $_SERVER['REQUEST_URI'];
This works just fine
if (strpos($a, 'en' ) == true)
But this doesn't
if (strpos($a, '/en/' ) == true) - Doesn't work
I tried a lot of things to escape the character or format the string but as it seems I am too stupid...
The issue is that strpos returns the position or FALSE if not found.
So if the url is /en/something/some then you are hitting the situation where en is a position 1 and any non-zero number is true
When you do /en/ then the starting position is 0 and that is false.
you need to check with === in the end or more accurately !== example
<?php
$a= "/en/blo";
if (strpos($a, '/en/' ) !== false){
echo "TRUE";
} else {
echo "FALSE";
}

PHP Conditional to see if string contains a variable [duplicate]

This question already has answers here:
Problem with Strpos In PHP
(3 answers)
PHP strpos() function returning incorrect result
(1 answer)
Closed 5 years ago.
I'm struggling to figure out how to use PHP strpos to find a variable in a string. The code below works if I enter a number directly but not if I replace the number with a variable I know to be a single number. No PHP errors present but the conditional renders false. I've been searching all over and am stumped by this.
The $string variable returns "253,254,255".
The $current_page_id variable returns "253".
$string = "";
foreach( $page_capabilities as $post):
$string .= $post->ID.',';
endforeach;
if (strpos($string, $current_page_id) !== false) {
// This works
//if (strpos($string, '253') !== false) {
// This does not
if (strpos($string, $current_page_id) !== false) {
echo 'true';
}
}
You don't need that CSV string to check for the ID you're looking for. You can just do it in the foreach loop where you're currently building the string.
foreach ($page_capabilities as $post) {
if ($post->ID == $current_page_id) {
echo 'true';
break; // stop looking if you find it
}
}
You should cast $current_page_id to a string:
if (strpos($string, (string)$current_page_id) !== false) { ...
The following is from php docs:
If needle is not a string, it is converted to an integer and applied
as the ordinal value of a character.
Which means you were comparing "253,254,255" with the character corresponding to 253 in the ASCII table
You need to cast the integer to a string.
$postIDs = [55, 89, 144, 233];
$string = implode(',', $postIDs);
$postID = 55;
if(strpos($string, strval($postID)) !== false) {
echo 'strpos did not return false';
}
I would suggest you to change your approach. You should search for the exact string value. Because as #mickmackusa suggested, it would find 25 & 53 too
$page_capabilities = array(array('POSTID' => 253),array('POSTID' => 254),array('POSTID' => 255));
$current_page_id = '255';
$key = array_search($current_page_id, array_column($page_capabilities, 'POSTID'));
if($key !== false) {
echo 'true';
}

String to int/float [duplicate]

This question already has answers here:
checking if a number is float in PHP
(5 answers)
Shortest way to check if a variable contains positive integer using PHP?
(6 answers)
Closed 5 years ago.
I have a number that comes as a string from an input. It could be "450.021" or "30". I need to determine whether it is int or a float number. How do I do that?
I tried with intval but apparently floating numbers are floored and in my case it is not going to help me.
Very naiv approach
foreach(["342.23", "30"] as $a) {
if( (float)(int)$a === (float)$a ) {
echo "int";
} else {
echo "float";
}
}
But this won't catch cases where you parse "340.0".
Edit: Digging a bit more:
foreach(["342.23", "30"] as $a) {
$is_float = filter_var($a, FILTER_VALIDATE_FLOAT);
$is_int = filter_var($a, FILTER_VALIDATE_INT);
if( $is_float && $is_int ) {
echo $a . " is Int";
} else if( $is_float && !$is_int ) {
echo $a . " is Float";
}
}

strpos() madness- why doesn't it work? [duplicate]

This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 7 years ago.
See the following issue:
$str = "video-23984"; // returns false
$str = " video-23984"; // returns true
$search= "video";
if(strpos($str,$search)) {
echo "True";
}else {
echo "False";
}
Why in the world does $str = "video-23984" return false? And what can I do to make it return true?
In the first string the word video is in the first position, which means 0.
In the second it's in the second position, which means 1.
Since you're returning it into an if, the 0 is a boolean for false. That's why you're getting false as a result.

Determining if string ends with another string [duplicate]

This question already has answers here:
startsWith() and endsWith() functions in PHP
(34 answers)
Closed 9 years ago.
How do one determine,if the strings appears at the end of the other string. If they do then print true to standard out and false if they don’t. Would strpos will help?
Sample Input
S1: “staff”
S2: “FF”
how would i make a function to run this,
function matching_ends($s1,$s2){
}
<?php
$s1="testing";
$s2="ing";
echo matching_ends($s1, $s2);
function matching_ends($s1,$s2){
return substr($s1, -strlen($s2)) == $s2 ? "true" : "false";
}
?>
Demo
You can use this
if( substr($s1, strlen($s1)-1) === substr($s2, strlen($s2)-1))
{
// Do something when character at the last matches
}
else{
// Do something when doesn't match
}

Categories