Jump to content

Finding position of a slash in a string


shah

Recommended Posts

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

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.