This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 9 years ago.
I'am looking for a syntax that will do the following:
if('String' not in $string){
//do this
}
I am also looking for away to say
if('String' in $string){
//do this
}
Can anyone help, I have googled for solutions and they seem very confusing.
Thanks,
J
I always use this:
if (stristr($string, "string")) {
//String exists within $string
}else{
//String does not exist
}
http://www.php.net/stristr
Try this:
if(strpos($string, 'String') !== false) {
//string exist
}
else{
//string not exist
}
Related
This question already has answers here:
Check if variable starts with 'http'
(6 answers)
Closed 3 years ago.
I have a variable and i want to check if it starts with 'pa_' how can I do that?
i have tried this but it does not work
$test_str = 'pa_';
if(substr( $product_attribute['name'], 0, strlen($test_str) ) === $test_str) {
$pa_array[]= $product_attribute['name'];
}
Just check if pa_ is at the first position of the string
if (strpos($product_attribute['name'], 'pa_') === 0) {
$pa_array[]= $product_attribute['name'];
}
Try dd($pa_array) within the if-block to see if you even put it in the array.
I quickly tried this and it worked.
Use strpos or access chars in a string by using an offset value:
$paName = $product_attribute['name'];
if($paName[0] . $paName[1] . $paName[2] == $test_str) {
$pa_array[]= $paName;
}
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.
This question already has answers here:
How to check if a string starts with a specified string? [duplicate]
(5 answers)
Closed 8 years ago.
I was looking and found How to check if variables starts with specific string & How to chech if a variable contains specific string but, I don't get it.
Which one could I use to check if a variable STARTS with a specific string just like a MYSQL LIKE WHERE CLAUSE:
SELECT * FROM table WHERE column LIKE 'string%'
You could use this, to match for any length string. Instead of hardcoding the string and it's length.
$string = "string to test";
$testfor = "strin";
if(substr( $string, 0, strlen($testfor) ) === $teststr) {
echo "Match";
}
if(substr($string, 0, 5) === "String") {
# Do code
} else {
# Error
}
Easy enough for simple strings.
This question already has answers here:
PHP - if condition inside string
(5 answers)
Closed 8 years ago.
I have a string like this:
$str = "0 || 0 && 1";
actually this string is a condition.
if i do like this :
if($str) {
echo "done";
}
else {
echo "sfcsd";
}
it is always true since $str is string.
How can i evaluate this string with out eval().
check out sandboxing in PHP. here is a quick tutorial:
http://www.fieryprophet.com/blog/detail/sandboxing-untrusted-code-with-phpsandbox
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
}