java拼接url时单引号,在url链接中转义单引号

I have a link that is sent throw some php:

echo "voir sa galerie";

$galerry links to another page.

get_title($primid) is the id of a specific element in $galerry page.

And the mechanism works fine until one of the elements id has a single quote in it . Which makes sense as it would interrupt the echo function.

This is why i have the apastro function:

function apastro($phrase){

$phrase1 = str_replace("'","\'",$phrase);

return $phrase1;

}

Yet the \ before the single quote isn't helping...

So Lets say the link redirects to the element with id="l'aro" on the page something.php

then the url will be something.php#l\

解决方案

it would interrupt the echo function

It wouldn't. It would break a string literal delimited by ' characters, but your string literal is delimited with " characters. In this case, it is breaking the HTML attribute value which is delimited by ' characters.

\ is not an escape character for URLs or HTML.

Use urlencode to make a string safe to put into a URL.

Use htmlspecialchars to make a string safe to put into an HTML attribute.

$title = get_title($primid);

$urlsafe_title = urlencode($title);

$url = $galerry . "#" . $urlsafe_title;

$htmlsafe_url = htmlspecialchars($url, ENT_QUOTES | ENT_HTML5);

echo "voir sa galerie";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值