Basically I have this url http://xxxxxxx.xxx/example.com/category-1sub-category-11/products.html and I have this string sub-category-11
and I want to add a slash before the string as such : 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);
Any help with this? Much appreciatd.
也许这个...
$new_url = str_replace($string, '/' . $string, $url);
You can use str_replace
for this purpose. Just replace category-1 with category-1/
<?php
$url = 'http://localhost/example.com/category-1sub-category-11/products.html';
$string = 'category-1';
$new_url = str_replace($string, $string.'/', $url);
or sub-category-11 with /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);
Hope this helps you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.