Jump to content

Finding position of a slash in a string


shah

Recommended Posts

There must be something wrong with your script, strpos() certainly accepts / as a needle.

 

if ($pos = strpos('path/to/directory', '/')) {
    print 'at: '. $pos;
} else {
    print 'no pos';
}

 

prints "at: 4"

 

also, i need to escape special characters. i am using strpos and want to match the pattern ?q=, but it wont accept the ? character. I think it's a special character so kindly tell me how can i escape it.

 

Thanks

 

strpos() has no special characters:

if ($pos = strpos('path/to/directory?q=hello+world', '?')) {
    print 'at: '. $pos;
} else {
    print 'no pos';
}

 

prints "at: 17"

sounds more like you are using a preg_xxx or ereg_xxx function and are using / as the delimiter for the pattern (and not escaping the / in your pattern) which also explains that whole business with the question mark. 

 

In general, you would escape something by putting a \ before it.  But if you want specific help, post some code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.