如何在PHP preg_replace中剥离$ {1}?

时间:2019-04-09 06:28:19

标签: php html regex preg-replace

我正在尝试替换脚本和img标签的source(src)。我有'../filename.js',我想摆脱2个点,该怎么办?

<?php

$file_path = content_url() . '/help/WS/WAS_B.htm';

$contents = wp_remote_fopen( $file_path );

$help_path = content_url() . '/help/';

$find = array(
    '#<script type="(.*?)" src="(.*?)">(.*?)</script>#is',
    '/<img src="(.*)" alt="(.*)" style="(.*)" \/>/i'
);

$replace = array(
    '<script type="${1}" src="' . $help_path . ' ${2}"></script>',
    '<img src="' . $help_path . '${1}" alt="${2}" style="${3}" />'
);

$preg_rep = preg_replace($find, $replace, $contents);

?>

这是我正在处理的链接和图像:

<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>

我想要获得的输出应该是:

<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>

1 个答案:

答案 0 :(得分:1)

您可以通过排除来自相应捕获组的那部分来“修剪”您得到的反向引用:

(?:\.\./)?(.*?)

将匹配并且不捕获 ../,并将其余的捕获到组中。

以下是代码修复:

$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} />'
);

请参见PHP demo

$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">

<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);

输出:

<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>