Jump to content

How to find "#" symbol in a String (URL)


preetham

Recommended Posts

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. 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

 

 

 

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.
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 by preetham
Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

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.

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.