2017-07-19 28 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

相關問題