How to check if a variable starts with a specific string? [duplicate] - php

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.

Related

PHP how to check if variable starts with 'pa_' or not? [duplicate]

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

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

Evaluate multiple conditions stored as a string withput eval method [duplicate]

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

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
}

PHP if not in string [duplicate]

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
}

Categories