2014-08-28 51 views
0

基本上我有这个网址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.htmlstr_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); 

对此有何帮助?非常赞赏。

+2

也许会更好不是一开始就把它建成这样,而不是改变它? – 2014-08-28 11:28:57

回答

1

也许这...

$new_url = str_replace($string, '/' . $string, $url); 
0

您可以使用str_replace用于此目的。只是属于1类/

<?php 
$url = 'http://localhost/example.com/category-1sub-category-11/products.html'; 
$string = 'category-1'; 
$new_url = str_replace($string, $string.'/', $url); 

或/子类别的11

<?php 
$url = 'http://localhost/example.com/category-1sub-category-11/products.html'; 
$string = 'sub-category-11'; 
$new_url = str_replace($string, '/' . $string, $url); 

望子类别的11替换类别-1这可以帮助你

相关问题