Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP str_replace to replace&;带有'的字符-';_Php_Regex - Fatal编程技术网

PHP str_replace to replace&;带有'的字符-';

PHP str_replace to replace&;带有'的字符-';,php,regex,Php,Regex,这是我的密码 $Meeting="4181211"; $EventID="Wanganui"; $Description="G Bristol & Sons (Bm75)"; $entities = array(' ', '%28', '%29'); $replacements = array('-',"(", ")"); echo str_replace($entities,$replacements, strtolower("https://www.ladbrokes.com.au/

这是我的密码

$Meeting="4181211";
$EventID="Wanganui";
$Description="G Bristol & Sons (Bm75)";
$entities = array(' ', '%28', '%29');
$replacements = array('-',"(", ")");
echo str_replace($entities,$replacements, strtolower("https://www.ladbrokes.com.au/racing/greyhounds/".$Meeting."/".$EventID."-".$Description."/"));
产出是这样的

https://www.ladbrokes.com.au/racing/greyhounds/4181211/wanganui-g-bristol-&-sons-(bm75)/ 
这很好,但在我的另一个例子中

$Meeting="4222658";
$EventID="Yonkers";
$Description="Yonkers Raceway F&M Clm Pce Ms";
$entities = array(' ', '%28', '%29');
$replacements = array('-',"(", ")");
echo str_replace($entities,$replacements, strtolower("https://www.ladbrokes.com.au/racing/greyhounds/".$Meeting."/".$EventID."-".$Description."/"));
输出类似于
https://www.ladbrokes.com.au/racing/greyhounds/4222658/yonkers-yonkers-raceway-f&m-clm pce ms/

但这里有一个问题,我希望我的输出像这样
https://www.ladbrokes.com.au/racing/greyhounds/4222658/yonkers-yonkers-raceway-f-m-clm-pce-ms/


我只想签入描述,如果没有空格(在“&”字符之后/之前),则应将其替换为“-”。例如,在f&m案例中,我想要像“f-m”,而Bristol&Sons则像Bristol-&Sons一样,这很好,()在输出中没有被%28和%29替换。有什么建议吗?

如果您只想在原始描述中用
-
替换
;在构建完整url之前,只需执行以下操作:

$Description=preg_replace("/(\w)&(\w)/",'$1-$2',"Yonkers Raceway F&M Clm Pce Ms");
// => //www.ladbrokes.com.au/racing/greyhounds/4222658/yonkers-yonkers-raceway-f-m-clm-pce-ms/
对于另一种情况,您可以得到:

$Description=preg_replace("/(\w)&(\w)/", "$1-$2", "G Bristol & Sons (Bm75)");
// => https://www.ladbrokes.com.au/racing/greyhounds/4222658/yonkers-g-bristol-&-sons-(bm75)/

谢谢,但我想替换为-,如果前后没有空间-。F&m应该像F-m一样,但布里斯托尔父子公司应该像布里斯托尔和索索克一样,我修改了帖子来处理这两种情况。这两者都应该正确。