views:

86

answers:

2

Hi

I have the following code

<a href="snippet:add?code=<?php echo rawurlencode($snippet->snippet_content); ?>Save snippet</a>

where

'$snippet = &lt;a rel=&quot;nofollow&quot; href=&quot;http://digg.com/submit?phase=2&amp;amp;url=&amp;lt;?php the_permalink(); ?&gt;&quot; title=&quot;Submit this post to Digg&quot;&gt;Digg this!&lt;/a&gt;'

How could I get rawurlencode to replace "&lt"; to a "<"?

Many thanks in advance

rob

updated

using

<?php echo rawurlencode(html_entity_decode($snippet->snippet_content)); ?>

as suggested by the posters below, thankyou fixes the changing &lt ; to "<" but inserts \ throughout the snippet

<a rel=\"nofollow\" href=\"http://delicious.com/post?url=&lt;?php the_permalink(); ?>&title=<?php echo urlencode(get_the_title($id)); ?>\" title=\"Bookmark this post at Delicious\">Bookmark at Delicious</a>

the output I'm seeking is without the backslashes aswell

<a rel="nofollow" href="http://delicious.com/post?url=&lt;?php the_permalink(); ?>&title=<?php echo urlencode(get_the_title($id)); ?>" title="Bookmark this post at Delicious">Bookmark at Delicious</a>

cheers rob

FIXED

Thankyou to all who posted!

<?php echo rawurlencode(htmlspecialchars_decode(stripslashes($snippet->snippet_content))); ?>

works a charm,

many thanks rob

+1  A: 

You should use the html_entity_decode() function to escape a &lt; to <.

But since this is a URL argument, you need to call rawurlencode() afterward, i.e.

<?php echo rawurlencode(html_entity_decode($snippet->snippet_content)); ?>
KennyTM
Hi, thanks for the quick response, if I use html_entity_decode then the code is printed/displayed on the page before the link text "Save Snippet". Any ideas?cheers rob
Rob Oliver
As background the '<a href="snippet:add' link type is much like the iTMS iTunes link allowing you to launch Snippet app if you have this installed and insert a snippet ($snippet) from a webpage.I've tried stripslashes, etc to no avail. rawurlencode at least keep the line breaks intact.cheers rob
Rob Oliver
@Rob: You need to call both functions. See update.
KennyTM
Hey thanks, how would I go about calling both functions in this case?cheers rob
Rob Oliver
Thankyou, I tried'<?php echo rawurlencode(html_entity_decode($snippet->snippet_content)); ?>'the output is now converted correctly but slashes are inserted -'<a rel=\"nofollow\" href=\"http://delicious.com/post?url=<?php the_permalink(); ?> ?>\" title=\"Bookmark this post at Delicious\">Bookmark at Delicious</a>'Any suggestions? cheers rob
Rob Oliver
@Rob: Not reproducible. You could add a `stripslashes()` if you like.
KennyTM
+1  A: 

rawurlencode() has nothing to do with converting to/from html-encoding. It performs URL encoding. The matching function to decode is rawurldecode(), but again, that is not what you're looking for here.

The &lt; encoding is html-encoding. To handle that, you want html_entity_decode() to decode or htmlentities() to encode.

Basic usage for the above sets of functions is:

$urlEncodedStr  = rawurlencode($str);
$urlDecodedStr  = rawurldecode($str);
$htmlEncodedStr = htmlentities($str);
$htmlDecodedStr = html_entity_decode($str);

To combine them together you would do some combination:

$urlEncodedHtmlDecodedStr  = rawurlencode(html_entity_decode($str));
JGB146