How do i make a if statement which checks if the string contains a forward slash?
$string = \"Test/Test\";
if($string .......)
{
mysql_query(\"\");
}
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
Use strpos()
If it doesn't return false, the character was matched.
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.