2017-07-19 33 views
0

我有一个PayPal按钮代码,我想使用自己的图像。这里是PayPal按钮代码:preg_replace输入标签中的特定src属性

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

...其中包含几个“输入”的标签,我想更换为输入(图像类型)的“SRC”。我已经创建了我的代码尽力而为:

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

但这不起作用。

可有人请点我在正确的方向(或者与正确的代码,或通过正则表达式工作的最简单的方式帮助页面!)

非常感谢!

回答

2

的方式去与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树,你所要做的就是连接根元素的所有子元素。

+0

谢谢 - 这工作完美...它目前在技术上超过了我的头(这是为什么我希望使用preg_replace或类似的东西进行排序),所以它不会是我可以重复使用的东西(!!)干杯:) – AndyiBM

+0

@AndyiBM:使用DOMDocument并不难,那里有时候有两三件事有点奇怪,你需要知道的就是这些。它在PHP手册中有详细记录(注释也很有用)。 XPath语言需要专用教程更难一点(请参阅http://www.zvon.org/comp/r/tut-XPath_1.html)。我会在我的答案中添加更多细节。 –

+0

@AndyiBM:其他优点,学习如何使用DOM不仅对PHP有用。一旦你知道它,你就可以在所有具有libxml实现的语言中使用它(非常接近所有语言和许多工具)。 –

0

为什么不只是做:

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

谢谢,但这是任务将由一个非技术人员完成,我想通过允许他们选择一个图像文件并在没有他们编辑的情况下进行交换来自动化HTML – AndyiBM

0

我建议你喜欢自己正在做你的HTML到不行。这真的是一个错误的路要走。

我建议你要么使用一个模板框架,一个简单的如智者 - 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

+0

谢谢 - 这可能是某种东西让我再看一次。 – AndyiBM

1

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

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

谢谢 - 这有效,但它也改变了我需要离开的第二个'src'属性。这就是为什么我将'src'指定为输入的一部分[type =“image”] – AndyiBM

+0

,您只需添加',1);'限制替换第一个,请参阅更新后的答案。 'input [type =“image”]'它用于css或javascript选择器,你想要的是'input type =“image”' – ewwink

+0

啊 - 是的,谢谢@ewwink,现在可以完美工作......我需要检查订单是否对贝宝按钮代码始终保持不变(如果他们的pixel.gif图像在任何点移动) – AndyiBM

相关问题