preetham Posted August 3, 2013 Share Posted August 3, 2013 Hi, Both my previous questions were perfectly solved here. Hoping the same this time. Problem :How to find "#" symbol in a String (URL) example : URL = http://www.myntra.com/sales?nav_id=1#!sortby=PRICEA In above link, there is a "#" symbol used, however in code, when i do echo strpos("www.myntra.com/sales?nav_id=1#!sortby=PRICEA","#"); I don't get any value at all. How can i find the pos of "#" in url Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/ Share on other sites More sharing options...
Harlequin Posted August 3, 2013 Share Posted August 3, 2013 I had a similar issue in which I needed to grab a part of a string from within a much longer string that changed but the start and end of the string would always remain the same so I used this:http://php.net/manual/en/function.strtok.php Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443337 Share on other sites More sharing options...
kicken Posted August 3, 2013 Share Posted August 3, 2013 echo strpos("www.myntra.com/sales?nav_id=1#!sortby=PRICEA","#"); outputs: 29 So your example works perfectly fine. Going on an assumption that you are trying to identify the # in the current url (ie, $_SERVER['REQUEST_URI']) then your problem is that the hash portion of the URL (the # and everything after it) is not sent to the server as it's strictly for client-side use. When the above example is requested the actual request made to the server is only for /sales?nav_id=1 and then once the page is loaded the browser interprets the #!sortby=PRICEA portion. Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443338 Share on other sites More sharing options...
preetham Posted August 3, 2013 Author Share Posted August 3, 2013 Hello, I'm sorry. Actually, I didn't give complete info. This how i actually do it. I send this link as parameter to my file. http://www.myntra.com/sales?nav_id=1#!sortby=PRICEA <?php $mylink = $_GET["link"]; if( stripos($mylink,"Myntra.com")>0) { echo strpos($mylink,"#"); } ?> It doesn't work in this case. Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443344 Share on other sites More sharing options...
kicken Posted August 3, 2013 Share Posted August 3, 2013 Works fine for me, so either you're still leaving out some information, or you fixed it during posting. var_dump your $mylink variable to make sure it contains what you expect. Make sure you are not making any typos such as a case-mismatch ($mylink vs $myLink for example). Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443347 Share on other sites More sharing options...
preetham Posted August 3, 2013 Author Share Posted August 3, 2013 (edited) Works fine for me, so either you're still leaving out some information, or you fixed it during posting. var_dump your $mylink variable to make sure it contains what you expect. Make sure you are not making any typos such as a case-mismatch ($mylink vs $myLink for example). No Typo. Did you try this code? <?php $mylink = $_GET["link"]; if( stripos($mylink,"Myntra.com")>0) { echo strpos($mylink,"#"); } ?> Bwowser treat "#" differently , this is a know issue. Try this echo (urlencode($mylink)); And when you check output, get this http%3A%2F%2Fwww.myntra.com%2Fsales%3Fnav_id%3D1 # and everything after that is ignored . Edited August 3, 2013 by preetham Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443358 Share on other sites More sharing options...
kicken Posted August 3, 2013 Share Posted August 3, 2013 (edited) kicken@web1:~$ php <?php $mylink = 'http://www.myntra.com/sales?nav_id=1#!sortby=PRICEA'; echo urlencode($mylink); ?> http%3A%2F%2Fwww.myntra.com%2Fsales%3Fnav_id%3D1%23%21sortby%3DPRICEA The # is still there, it's just been encoded as %23. As I said, var_dump your variable to verify what it actually contains. If the # and everything after it are infact missing, then you need to revisit my first point:...your problem is that the hash portion of the URL (the # and everything after it) is not sent to the server as it's strictly for client-side use Make sure you urlencode() your values before putting them into a link or header redirect so that special characters such as # are not mis-interpreted. Edited August 3, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443374 Share on other sites More sharing options...
preetham Posted August 4, 2013 Author Share Posted August 4, 2013 kicken@web1:~$ php <?php $mylink = 'http://www.myntra.com/sales?nav_id=1#!sortby=PRICEA'; echo urlencode($mylink); ?> http%3A%2F%2Fwww.myntra.com%2Fsales%3Fnav_id%3D1%23%21sortby%3DPRICEA I'm not hard coding the link it comes from _GET, so like you said in first post, everything after # is ignored. I wanted to get that. Quote Link to comment https://forums.phpfreaks.com/topic/280797-how-to-find-symbol-in-a-string-url/#findComment-1443395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.