基本上我有这个网址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。str_replace通过在一个字后面加一个斜杠php
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'sub-category-11';
$new_url = preg_replace('/\b'.$string.'\b/', '/'.$string, $url);
对此有何帮助?非常赞赏。
也许会更好不是一开始就把它建成这样,而不是改变它? – 2014-08-28 11:28:57