I am trying to find whether a string contains a certain text or not using strstr()
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr('/image/', $t));
exit;
But this gives false. Why is it giving fasle? How to fix it?
Your parameters are inverted (see strstr). This is the correct way of using it:
strstr($t, '/image/');
you should use strpos , faster, less resources, from the manual with your vars
<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
$findme = '/image/';
$pos = strpos($t, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>
try like this instead
<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>
You can see the documentation of the function.
Validates the syntax.
php/documentation/function.strstr.php
The correct use would: var_dump(strstr($t, '/image/'));
Related
I have problem with checking if part of string is containing other string but strpos and stripos are not working for me.
The case that I need to check if word pris is containing in string -med prista-
Any suggestions?
Try
$str = '-med prista-';
if (preg_match('/pris/', $str))
echo 'OK';
else
echo 'Not';
In PHP 8 you have a new function called str_contains() created for this purpose:
if (str_contains('-med prista-', 'pris')) {
echo "Found!";
}
If you are using older PHP versions then you can use mb_strpos (multibyte), or strpos functions like this:
if(mb_strpos('-med prista-', 'pris') === false){
// not found
} else {
echo "Found!";
}
So far, I managed to find if ONE letter is in ANY given string. However, I cannot manage to ask for multiple conditions like 'a' && 'e' && 'i'... In a way that all of those letter need to be in the string so the echo "Vowel found" gets out.
I have tried with stristr, strpos... and so far my best guess was deprecated so I couldn't use it.
My last resource, which didn't work anyway, was to nest if conditions to make it work; also tried with arrays with no positive result.
$string = 'murcielago'; // a word with 5 vowels
if (stristr($string, 'a') == TRUE) {
echo 'Vowel found';
} else {
echo "Vowel not found";
}
This is my code for ONE string or letter to be found in the original given string.
It made me think further! Got it.
$string = "murcielago";
if (strpos($string,'a') && strpos($string,'e') && strpos($string,'i') && strpos($string,'o') && strpos($string,'u') == true)
{
echo "nailed it";
} else {
echo "No vowels for you";
}
ยดยดยด
I wonder if there is any way to make all the conditions into one since I am still using the same strpos function.
Try using a regex and preg_match
<?php
//Enter your code here, enjoy!
$string = 'murcielago';
// This will echo "vowel found"
if(preg_match( '`[aeiouy]`', $string)){
echo "vowel found\n";
}
$string2 = 'bcdfgplk';
// This won't echo anything
if(preg_match( '`[aeiouy]`', $string2 )){
echo "vowel not found\n";
}
How do i make a if statement which checks if the string contains a forward slash?
$string = "Test/Test";
if($string .......)
{
mysql_query("");
}
else
{
echo "the value contains a invalid character";
}
You can use strpos, which will make sure there is a forward slash in the string but you need to run it through an equation to make sure it's not false. Here you can use strstr(). Its short and simple code, and gets the job done!
if(strstr($string, '/')){
//....
}
For those who live and die by the manual, when the haystack is very large, or the needle is very small, it is quicker to use strstr(), despite what the manual says.
Example:
Using strpos(): 0.00043487548828125
Using strstr(): 0.00023317337036133
if(strpos($string, '/') !== false) {
// string contains /
}
From the PHP manual of strstr:
Note:
If you only want to determine if a particular needle occurs within
haystack, use the faster and less memory intensive function strpos()
instead.
Use strpos()
If it doesn't return false, the character was matched.
I compared strpos() results with 0. Somehow comparison with false did not work for me.
if (strpos($t, '/') !== 0) {
echo "No forward slash!";
}
I want to know if how can I check I string (specifically an ip address) if that ip address has the string of x
For example
$ip = "66.124.61.23" // ip of the current user
$x = "66.124" // this is the string what I want to check in my $ip.
So how can I check $ip if it has the string of $x?
Please do leave a comment if you are having a hard time understanding this situation.
Thank you.
Use strstr()
if (strstr($ip, $x))
{
//found it
}
See also:
stristr() for a case insenstive version of this function.
strpos() Find the first occurrence of a string
stripos() Find the position of the first occurrence of a case-insensitive substring in a string
You can also use strpos(), and if you're specifically looking for the beginning of the string (as in your example):
if (strpos($ip, $x) === 0)
Or, if you just want to see if it is in the string (and don't care about where in the string it is:
if (strpos($ip, $x) !== false)
Or, if you want to compare the beginning n characters, use strncmp()
if (strncmp($ip, $x, strlen($x)) === 0) {
// $ip's beginning characters match $x
}
Use strstr()
$email = 'name#example.com';
$domain = strstr($email, '#');
echo $domain; // prints #example.com
Based on $domain, we can determine whether string is found or not (if domain is null, string not found)
This function is case-sensitive. For case-insensitive searches, use stristr().
You could also use strpos().
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
Also read SO earlier post, How can I check if a word is contained in another string using PHP?
Use strpos().
if(strpos($ip, $x) !== false){
//dostuff
}
Note the use of double equals to avoid type conversion. strpos can return 0 (and will, in your example) which would evaluate to false with single equals.
Lets say i capture the url as $url = $_SERVER['REQUEST_URI'] or $_GET['q'].
How can i check for the condition if the url contains a piece of text say "x"?
What regex would be required for it
If you use preg_match with a variable value, make sure you use preg_quote to escape any special characters:
$look_for = "x";
if (preg_match( "/".preg_quote($look_for, "/")."/i" , $url) ) {
...
You can also use the strpos() function
http://php.net/manual/en/function.strpos.php
This is one example from there:
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
In addition to the functions for finding the string, you may also want to check out Drupal's arg() function: http://api.drupal.org/api/drupal/includes--path.inc/function/arg/6
I think preg_match would serve you well:
if (preg_match( "/x/i" , $url) ) {
}
If your working with a url from the wild you might want to use php's parse_url(), http://www.php.net/manual/en/function.parse-url.php , that will make it easy to work with. If it is a drupal URL your probably going to want to use Drupal's arg() function, http://api.drupal.org/api/drupal/includes--path.inc/function/arg/6 , but it can have issues with the Pathauto module