PHP-替换撇号

PHP-替换撇号,php,string,url,replace,apostrophe,Php,String,Url,Replace,Apostrophe,我目前正在开发一个网站的名单。一些名称包括撇号”,我想用它们的名称将它们链接到一个网站 我想链接到url,如: example.com/(他们的名字) 通过这样做,我首先将“”替换为“+”。所以链接看起来像:example.com/john+doe 但是如果名称是John'Doe,它会将url转换为example.com/John 并跳过姓氏 我怎样才能解决这个问题?我试着将',\'等更改为html代码,更改为',等等,但似乎没有任何效果 这是我目前的代码: $name = $row['name

我目前正在开发一个网站的名单。一些名称包括撇号
,我想用它们的名称将它们链接到一个网站

我想链接到url,如: example.com/(他们的名字)

通过这样做,我首先将“”替换为“+”。所以链接看起来像:example.com/john+doe

但是如果名称是John'Doe,它会将url转换为example.com/John

并跳过姓氏

我怎样才能解决这个问题?我试着将
'
\'
等更改为html代码,更改为',等等,但似乎没有任何效果

这是我目前的代码:

$name = $row['name'];
$new_name = str_replace(
    array("'", "'"),
    array(" ", "+"),
    $name
);

echo "<td>" . $name . " <a href='http://www.example.com/name=" . $new_name . "' target='_blank'></a>" . "</td>";
它将空格更改为+,但如何修复撇号?有人知道吗

echo urlencode("John'Doe Johnson");
返回

John%27Doe+Johnson
返回

John%27Doe+Johnson

您应该使用PHP的函数
urlencode
,PHP.net/manual/en/function.urlencode.PHP

<?php
$name = $row['name'];
//$urlname = urlencode('John\'Doe Johnson');
$urlname = urlencode($name);
echo "<td>$name<a href='http://www.example.com/name=$urlname' target='_blank'>$name</a></td>";

您应该使用PHP的函数
urlencode
,PHP.net/manual/en/function.urlencode.PHP

<?php
$name = $row['name'];
//$urlname = urlencode('John\'Doe Johnson');
$urlname = urlencode($name);
echo "<td>$name<a href='http://www.example.com/name=$urlname' target='_blank'>$name</a></td>";

example.com服务器似乎出现了问题。撇号字符是URL中的有效字符。请参阅:example.com服务器似乎发生了一些问题。撇号字符是URL中的有效字符。见: