仅更改包含change.com的网址,将“ /”替换为“-”,并将.html放在末尾
<a href="http://www.notchange.com/adf/i18n/wiki/" class="coasfs" >as3rc</a>
<a href="http://www.change.com/q/photoshopbattles/comnts/2n4jtb/psbattle_asgfdhj/" class="coasfs" >as3rc</a>
<a href="http://www.change.com/q/photottles/commes/" class="coefs" >ase3rc</a>
我需要下面的结果链接
http://www.change.com/q-photoshopbattles-comments-2n4jtb-psbattle_asgfdhj.html
请帮助我,我用正则表达式尝试了很多次,但都失败了。
这是完成此操作的一种方法,但是它结合使用了Regexes和PHP函数。此方法将比纯正则表达式解决方案简单。
$string = '<a href="http://www.notchange.com/adf/i18n/wiki/" class="coasfs" >as3rc</a>'
. '<a href="http://www.change.com/q/photoshopbattles/comnts/2n4jtb/psbattle_asgfdhj/" class="coasfs" >as3rc</a>'
. '<a href="http://www.change.com/q/photottles/commes/" class="coefs" >ase3rc</a>';
//The regex used to match the URLs
$pattern = '/href="(http:\/\/www.change.com\/)([^">]*)"/';
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
//trim the ending slash if exists, replace the slashes in the URL path width a -, and add the .html
$newUrl = $val[1] . str_replace("/", "-", trim($val[2], "/")) . '.html';
$string = str_replace($val[0], 'href="' . $newUrl . '"', $string);
}
echo $string;
我使用了正则表达式来帮助查找需要修改的URL,然后使用PHP必须完成的某些内置函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句