基本上我有这个网址http://xxxxxxx.xxx/example.com/category-1sub-category-11/products.html,我有这个字符串sub-category-11
,我想在字符串之前添加一个斜杠:http://xxxxx.xxx/example.com/category-1/sub-category-11/products.html。
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'sub-category-11';
$new_url = preg_replace('/\b'.$string.'\b/', '/'.$string, $url);
对此有何帮助?很感激。
答案 0 :(得分:1)
也许这......
$new_url = str_replace($string, '/' . $string, $url);
答案 1 :(得分:0)
您可以将str_replace
用于此目的。只需将category-1替换为category-1 /
<?php
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'category-1';
$new_url = str_replace($string, $string.'/', $url);
或子类别-11与/ sub-category-11
<?php
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'sub-category-11';
$new_url = str_replace($string, '/' . $string, $url);
希望这有助于你