我几乎有我的正则表达式为了浏览html页面,但遇到了两个问题,我试图在我继续之前得到压扁,我需要能够匹配空和斜杠(和空关闭报价),但已经用尽了我的能力,看看我在做什么,有人可以帮助我的最后一点?PHP正则表达式缺少两个匹配
$pathspec='in-front';
$subjects = array(
'<base href="http://foo.com/images/" target="_blank">', # no changes (correct)
'<base href="/" target="_blank">', # '/in-front/' (fails)
'<a href="https://foo.com/images/">Foo</a>', # no changes (correct)
'<a href="">Foo</a>', # '/in-front/' (fails)
'<img src="bar/foo.png" />', # no changes (correct)
'<img src="/bar/foo.png" />', # '/in-front/bar/foo.png' (correct)
);
foreach ($subjects AS $subject)
echo preg_replace('/(href|src)=["\']?\/(?!\/)([^"\'>]+)["\']?/', "$1='/$pathspec/$2'", $subject) . "\n";
die;
预期的输出是在评论部分,谢谢。
@php_nub_qq:嗯什么? –
所以基本上你想要的是将'$ pathspec'添加到任何空或根href属性,是吗? –
@php_nub_qq关闭,仔细阅读正则表达式,以及预期的输出及其输入。 – ehime