我需要从url中删除第二次出现的子字符串。所以,我的网址是这样的:
http://example.com/<category>/<category>-<subcategory>-<id>
e.g。
http://example.com/laptop/laptop-hp-probook-101 must be http://example.com/laptop/hp-probook-101
或
http://example.com/computer-parts/computer-parts-cool-motherboard-123 must be http://example.com/computer-parts/cool-motherboard-123
我试过了:
$new_url = preg_replace("$category", "", absoluteurl(<url>), 1);
但它失败了......
答案 0 :(得分:1)
您可以使用反向引用:
<?php
$str = 'http://example.com/laptop/laptop-hp-probook-101';
//must be http://example.com/laptop/hp-probook-101
$str = preg_replace('/\/(.*?)\/\1-/', '/$1/', $str);
print $str;
输出:
http://example.com/laptop/hp-probook-101
答案 1 :(得分:0)
试试这个
<?php
$text = 'This is a test';
echo substr_count($text, 'is'); // 2
答案 2 :(得分:0)
$url='https://www.yourdomain.com/laptops/laptops-hp-laptop-laptopid';
$arr1=explode("://",$url);
$arr2=explode("/",$arr1[1]);
$arr2[2]=str_replace ( $arr2[1]."-" , "" , $arr2[2] );
$s2=implode("/",$arr2);
$newurl=$arr1[0]."://".$s2;
echo $newurl;
尝试以上所有内容