我要删除图像源(src)中的任何数量的“ ../”。
我有一个正则表达式,它删除了“ ../”,并且如果源代码中没有“ ../”,它也可以工作。
<?php
$help_path = 'http:xxx.com/wp-content/help/';
$contents = <<<MYVAR
<img src="Links/WAS_PIC_ControlBox-1-2-3.jpg" alt="WAS-Betjeningsboks-1-2-3"
style="border: none; margin-left: 20px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;" border="0">
<img src="../Links/WAS_PIC_ControlBox-1-2-3.jpg" alt="WAS-Betjeningsboks-1-2-3"
style="border: none; margin-left: 20px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;" border="0">
<img src="../../Links/WAS_PIC_ControlBox-1-2-3.jpg" alt="WAS-Betjeningsboks-1-2-3"
style="border: none; margin-left: 20px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;" border="0">
<script type="text/javascript" src="ehlpdhtm.js"></script>
<script type="text/javascript" src="../ehlpdhtm.js"></script>
<script type="text/javascript" src="../../ehlpdhtm.js"></script>
MYVAR;
$find = array(
'#<script\s+type="(.*?)"\s+src="(?:\.{2}/)?(.*?)">(.*?)</script>#is',
'#<img\s+src="(?:\.{2}/)?(.*?)"\s+alt="(.*?)"([^>]*?)/?>#i'
);
$replace = array(
'<script type="${1}" src="' . $help_path . '${2}"></script>',
'<img src="' . $help_path . '${1}" alt="${2}"${3} />'
);
$preg_rep = preg_replace($find, $replace, $contents);
print_r($preg_rep);
问题是,如果src中有多个“ ../”,我的正则表达式将输出“ ../”。无论源中有多少个“ ../”(“../../../../”),它都应该始终像src =“ Links / xxx.jpg”。
<img src="http:xxx.com/wp-content/help/Links/WAS_PIC_ControlBox-1-2-3.jpg" alt="WAS-Betjeningsboks-1-2-3"
style="border: none; margin-left: 20px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;" border="0" />
<img src="http:xxx.com/wp-content/help/Links/WAS_PIC_ControlBox-1-2-3.jpg" alt="WAS-Betjeningsboks-1-2-3"
style="border: none; margin-left: 20px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;" border="0" />
<img src="http:xxx.com/wp-content/help/../Links/WAS_PIC_ControlBox-1-2-3.jpg" alt="WAS-Betjeningsboks-1-2-3"
style="border: none; margin-left: 20px; margin-right: 0px; margin-top: 0px; margin-bottom: 0px;" border="0" />
<script type="text/javascript" src="http:xxx.com/wp-content/help/ehlpdhtm.js"></script>
<script type="text/javascript" src="http:xxx.com/wp-content/help/ehlpdhtm.js"></script>
<script type="text/javascript" src="http:xxx.com/wp-content/help/../ehlpdhtm.js"></script>
答案 0 :(得分:1)
您可以使用preg_replace
删除所有../
$contents = str_replace('../', '', $contents);
然后,您可以应用正则表达式
$contents = str_replace('../', '', $contents);
$find = array(
'#<script\s+type="(.*?)"\s+src="(?:\.{2}/)?(.*?)">(.*?)</script>#is',
'#<img\s+src="(?:\.{2}/)?(.*?)"\s+alt="(.*?)"([^>]*?)/?>#i'
);
$replace = array(
'<script type="${1}" src="' . $help_path . '${2}"></script>',
'<img src="' . $help_path . '${1}" alt="${2}"${3} />'
);
$preg_rep = preg_replace($find, $replace, $contents);