提问人:bledi 提问时间:9/4/2023 最后编辑:mickmackusabledi 更新时间:9/9/2023 访问量:61
替换 HTML 页面文本中找到的 url 文件路径的一部分
Replace portion of url filepath found in HTML page text
问:
给定html页面中的一些网址,我想替换一些网址,如下所示:
示例 url:从那个开始,我想用另一个文本 (https://example.com/cost-center/sub-one/article1
/cost-center/
article1
test
)
这意味着上面的 url 将转换为:。https://example.com/cost-center/test/article1
就我而言,之后可以有更多部分,url 可以以斜杠结尾,也可以在引号内,如下例所示:/cost-center/
https://example.com/cost-center/sub-one/sub-two/article-3/
https://example.com/cost-center/sub-one/sub-three/article-4
https://example.com/cost-center/sub-1/sub-two/sub-three/article-5/
'https://example.com/cost-center/sub-one/sub-two/article-3/'
'https://example.com/cost-center/sub-1/sub-two/sub-three/article-5'
"https://example.com/cost-center/sub-one/sub-three/article-4"
"https://example.com/cost-center/sub-1/sub-two/sub-three/article-5/"
这些将被替换如下:
https://example.com/cost-center/test/article-3/
https://example.com/cost-center/test/article-4
https://example.com/cost-center/test/article-5/
'https://example.com/cost-center/test/article-3/'
'https://example.com/cost-center/test/article-5'
"https://example.com/cost-center/test/article-4"
"https://example.com/cost-center/test/article-5/"
现在,我们假设 url 在 /cost-center/
;
例如https://example.com/cost-center/sub-1/sub-two/sub-three/article-5/
所以基本上我想在保留最后一部分的同时替换它的某些部分。
我尝试使用数字正则表达式,例如:
preg_replace('~https://example.com/cost-center/[^/]+/([^/]+)~', 'https://example.com/cost-center/test/$1', $url);
preg_replace('/(["\']?)(https:\/\/[^\/]+\/)([^\/]+)(\/[^"\s]*)?/', '$1$2test$4$1', $url);
我也尝试过使用拆分 url 并逐个手动解析它,但结果非常复杂和丑陋。explode
也没有好的结果。ChatGPT
答:
我尝试了以下方法: 正则表达式捕获 3 组:
- url 的开头
/cost-center/
- url 的最后一部分之间的所有内容
/cost-center/
- url 的最后一部分,可以以斜杠结尾,也可以不以斜杠结尾
$pattern = '/(https:\/\/example.com\/cost-center\/)(.*?)([^\/]+\/?$)/';
$replacement = '$1test/$3';
$result = preg_replace($pattern, $replacement, $url);
在替换字符串中,保留第一个组和第三个组,并将第二个组替换为 test/。它将 url 和 url 的最后一部分之间的文本替换为($1 and $3)
/const-center/
test/
编辑:我修改了正则表达式以包含引号和属性名称,以防 URL 位于 HTML 属性(如 href)中
$pattern = '/(href=["\']https:\/\/example.com\/cost-center\/)(.*?)([^\/]+\/?["\'])/';
评论
'href="https://example.com/cost-center/sub-one/sub-two/article-3/"'
href
'href="https://example.com/cost-center/sub-one/sub-two/article-3" data-id="5"'
article-3" data-id="5"'
从您对任务的描述和示例数据来看,URL 是否/如何用引号换行实际上并不重要。您只需要匹配 URL 的前导部分以验证它是否是 URL,然后隔离不需要的子字符串并替换它。
请注意,我的替换值只是字符串,没有对捕获组的引用。这是因为会忘记/释放到该点为止匹配的所有字符,并且是一种预告,这意味着它不会消耗任何匹配的字符。test
\K
(?= ... )
至于隔离要替换的模式部分,我使用一个包含正斜杠和空格的否定字符类,然后是一个字面上的正斜杠。该子模式可能会多次重复一个矿石(因为量词)。+
代码:(演示)
echo preg_replace('#https://[^/]+/cost-center/\K([^/\s]+/)+(?=article)#', 'test/', $text);
评论