Skip to content
Advertisement

If string contains forward slash

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement