str_替换为在单词php后添加斜杠

str_替换为在单词php后添加斜杠,php,preg-replace,Php,Preg Replace,基本上,我有这个url,我有这个字符串sub-category-11,我想在字符串前面加一个斜杠: 有什么帮助吗?非常感谢。也许这 $new_url = str_replace($string, '/' . $string, $url); 为此,您可以使用str\u replace。只需将类别1替换为类别1/ <?php $url = 'http://localhost/example.com/category-1sub-category-11/products.html'; $stri

基本上,我有这个url,我有这个字符串
sub-category-11
,我想在字符串前面加一个斜杠:

有什么帮助吗?非常感谢。

也许这

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

为此,您可以使用
str\u replace
。只需将类别1替换为类别1/

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

也许一开始不这样构建它比改变它要好?
<?php
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'category-1';
$new_url = str_replace($string, $string.'/', $url);
<?php
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'sub-category-11';
$new_url = str_replace($string, '/' . $string, $url);