Php 替换url中使用&;的字符;

Php 替换url中使用&;的字符;,php,function,character-encoding,google-places-api,Php,Function,Character Encoding,Google Places Api,我正在使用GooglePlacesAPI…我正在尝试存储一个带有&(符号)的位置。我似乎无法对变量进行编码。例如,企业名称称为Dave&Busters,但我可以在url中使用&作为请求url,因为这是为参数保留的。我尝试使用这两个函数替换&if-find。也许我用错了 $name = 'Dave & Busters'; if (strpos($name, '&') !== false) { //& SIGN FOUND //WONT WORK FOR URL NEED

我正在使用GooglePlacesAPI…我正在尝试存储一个带有&(符号)的位置。我似乎无法对变量进行编码。例如,企业名称称为Dave&Busters,但我可以在url中使用&作为请求url,因为这是为参数保留的。我尝试使用这两个函数替换&if-find。也许我用错了

$name = 'Dave & Busters';
if (strpos($name, '&') !== false) {
//& SIGN FOUND
 //WONT WORK FOR URL NEED TO REPLACE
$name = str_replace('&', 'and', $string);

}
$name = urlencode($name);
$url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10&type=restaurant&keyword=$name&key=myKey";
编辑*只需使用urlencode()我就可以得到这个。Dave+只显示

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7455483,-73.5951152&radius=10&type=restaurant&keyword=Dave+&key=AIzaSyD4rcVpAgnP650-DCeL2L4zTnPSo4d81vk

对于查询字符串中使用的任何数据,都需要使用
urlencode()

更好的方法是使用http\u build\u query():


对于查询字符串中使用的任何数据,都需要使用
urlencode()

更好的方法是使用http\u build\u query():


我正在使用urlencode,但它一直向Dave+输出数据only@Carlitos在
urlencode()
之前停止替换东西。我也尝试过这样做。我没有得到任何回应,因为参数与dave&busters符号和$url=“”)混淆;非常感谢。这是我的错误。jqueryajax并没有完全超越Dave&Busters。戴夫的电话一直中断。我必须用encodeURIComponent()对变量进行编码。然后它终于顺利工作了。我正在使用urlencode,但它一直输出到Dave+only@Carlitos在
urlencode()
之前停止替换东西。我也尝试过这样做。我没有得到任何回应,因为参数与dave&busters符号和$url=“”)混淆;非常感谢。这是我的错误。jqueryajax并没有完全超越Dave&Busters。戴夫的电话一直中断。我必须用encodeURIComponent()对变量进行编码。然后它终于顺利地工作了。
$url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' . http_build_query([
  'location' => $lat . ',' . $long,
  'radius' => 10,
  'type' => 'restaurant',
  'keyword' => $name,
  'key' => 'myKey'
]);