preg_replace 输入标签中的特定 src 属性

preg_replace specific src attribute in input tag

我有一个 PayPal 按钮代码,我想为其使用我自己的图像。这是 PayPal 按钮代码:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"> 
    <input type="hidden" name="cmd" value="_s-xclick"> 
    <input type="hidden" name="hosted_button_id" value="Q8LQ82SNNSBKC"> 
    <input type="image" src="https://www.paypal.com/images/pp-buy-now-btn-bryellow.png" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!"> 
    <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1"> 
</form>

...其中包含几个 'input' 标签,我想将 'src' 替换为输入(图像类型)。我已尽最大努力编写代码来执行此操作:

$paypal_button_code2 = preg_replace('/<input[type="image"]\ [^>]+src="([^"]+)"/', $newimageurl, $paypal_button_code);

但这不起作用。

有人能给我指明正确的方向吗(使用正确的代码,或者帮助页面以简单的方式处理正则表达式!!)

非常感谢!

为什么不直接做:

<input type="image" src="<?php echo $newimageurl;?>" ...>

我建议您不要像现在这样处理 HTML。这条路真的走错了。

我建议您使用模板框架,一个简单的模板框架,例如 smarty - https://www.smarty.net/ - 或者至少将您的字符串格式化为:

$my_url = "http://whatyouwant";
$whatever_html = <<<EOT
<input type="image" src={%MYCOOKIE%} border="0" name="submit" alt="PayPal – The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1"> </form>
EOT;

$whatever_html = str_replace($whatever_html, $my_url, "{%MYCOOKIE%}");

具有三重“<<<”的特殊语法 (HEREDOC) 允许您放置任何类型的 HTML 代码,而无需进一步警惕引号。

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

你不需要<input[type="image"]

$paypal_button_code2 = preg_replace('/src="([^"]+)/', 'src="'.$newimageurl, $paypal_button_code, 1);

使用 html 的方法是 DOM 文档,而不是正则表达式:

$html = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="Q8LQ82SNNSBKC"> <input type="image" src="https://www.paypal.com/images/pp-buy-now-btn-bryellow.png" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1"> </form>';

$dom = new DOMDocument;
$dom->loadHTML('<div id="root">' . $html . '</div>');
$root = $dom->getElementById('root');
$xp = new DOMXPath($dom);

$nodeList = $xp->query('//form[@action="https://www.paypal.com/cgi-bin/webscr"][1]/input[@type="image"]/@src');

if ( $nodeList ) {
    $newimageurl = 'path/to/newimage.gif';
    $nodeList->item(0)->nodeValue = $newimageurl;
    $html = '';

    foreach ($root->childNodes as $childNode) {
        $html .= $dom->saveHTML($childNode);
    }
}

echo $html;

XPath 查询详细信息:

//    # anywhere in the DOM tree
form  # a form element
[     # open a predicate (condition on the current element)
    # must have an attribute "action" with the value of this string
    @action="https://www.paypal.com/cgi-bin/webscr"
]
[1] # other predicate: first occurrence of this kind of form
/   # direct child
input
[
    @type="image"
]
/
@src # src attribute

关于'<div id="root">' . $html . '</div>'

DOMDocument::loadHTML 加载一个 html 字符串并构建 DOM 树。为此,您需要一个根元素,但由于您处理的是 html 部分而不是整个 html 文档,因此您的字符串可能未包含在唯一元素(即 <html>...</html> 在整个 html 文档中)。为了避免来自 DOMDocument 的自动更正,技巧在于提供一个伪造的根元素。一旦编辑了 DOM 树,您所要做的就是连接根元素的所有子元素。