replace entites in rawurlencode i.e. < > "

狂风中的少年 提交于 2019-12-10 15:28:42

问题


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;url=&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=<?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=<?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:


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



回答2:


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)); ?>


来源:https://stackoverflow.com/questions/3483422/replace-entites-in-rawurlencode-i-e-lt-gt-quot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!