Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"; 
} 
like image 657
Msmit1993 Avatar asked Jun 27 '12 11:06

Msmit1993


People also ask

How do I check if a string has a slash?

Method 1: Check if String Contains Backslash Using includes() Method. The JavaScript “includes()” method is used to check whether a character or a string is included in the string or if it exists in the string or not. You can use it to check for a backslash in a string.

How do I check if a string has a forward slash in Python?

Use the syntax "/" in row[7] , it's much more Pythonic.

What does forward slash do in regex?

The forward slash character is used to denote the boundaries of the regular expression: ? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters.

How do you escape a slash in a string?

In the platform, the backslash character ( \ ) is used to escape values within strings. The character following the escaping character is treated as a string literal.


1 Answers

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

like image 63
Mike Mackintosh Avatar answered Oct 01 '22 08:10

Mike Mackintosh