2013-02-05 45 views
0

我的客戶希望能夠將PayPal加載到購物車按鈕放置在頁面的任何位置,並且可能在單個頁面上有多個按鈕。他將使用[MONKEY]價格之類的短代碼,以PaylPal按鈕替換該文本,並附上「猴子」的價格和描述。在頁面上多次用HTML替換shortcode

因此,每當我們在頁面上找到[價格]簡碼時,我需要查詢數據庫以獲取價值,然後插入以下PayPal按鈕代碼,並在頁面上的該位置顯示按鈕。然後移動到下一個替換品,如果有的話。

這裏的PayPal按鈕代碼:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<input type="hidden" value="_cart" name="cmd" /> 
<input type="hidden" value="[email protected]" name="business" /> 
<input type="hidden" value="1" name="add" /> 
<input type="hidden" value="MONKEY" name="item_name" /> 
<input type="hidden" value="" name="item_number" /> 
<input type="hidden" value="17.00" name="amount" /> 
<input type="hidden" value="2" name="no_shipping" /> 
<input type="hidden" value="USD" name="currency_code" /> 
<input border="0" type="image" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it's fast, free and secure!" /> 
</form> 

我一直在試圖用preg_replace_callback()函數來做到這一點,但有將所有的HTML按鈕的問題,然後尋找更多的簡碼實例在頁面上並替換它們。

感謝您的幫助!在Wordpress中,所有這些代碼都已經寫好了(但在哪裏?) - 這是我從頭開始創建的應用程序,現在需要一些改進。謝謝!

這裏是我的正則表達式:

$pattern = '/\[price (.*?)\]/'; 
+0

你可以顯示你的正則表達式嗎? –

+0

$ pattern ='/ \ [price(。*?)\] /'; –

+0

好吧..當我粘貼上述消息時,反斜槓消失了。 –

回答

0

你可以只使用一個基本的preg_replace使用模式來完成這個任務。像這樣:

<?php 

function myexamplefunction($html){ 
    $pattern = '/\[price (.*?)\]/'; 
    $replace = '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
     <input type="hidden" value="_cart" name="cmd" /> 
     <input type="hidden" value="[email protected]" name="business" /> 
     <input type="hidden" value="1" name="add" /> 
     <input type="hidden" value="$1" name="item_name" /> 
     <input type="hidden" value="" name="item_number" /> 
     <input type="hidden" value="17.00" name="amount" /> 
     <input type="hidden" value="2" name="no_shipping" /> 
     <input type="hidden" value="USD" name="currency_code" /> 
     <input border="0" type="image" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it\'s fast, free and secure!" /> 
     </form>'; 
    return preg_replace($pattern,$replace,$html); 
} 
+0

因此,每次在單個頁面上都會顯示相同的按鈕。我需要從數據庫中提取價格和描述的值。然後,單個頁面上的短代碼的每個實例都需要用上面的代碼替換,並用該商品的價格和描述進行定製。 –

0

好的。 。 。我非常接近。當我運行下面的代碼,我得到一個警告:

Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2 

這裏是通過在網頁上找到的所有簡碼的數組代碼迴路,以及1)匹配[價格]簡碼,2)從中提取數據數據庫和構建PayPal按鈕的代碼,以及3)用HTML代碼替換短代碼。

$text = "<p>This is some text. This is the price: [price BOWBAG10.6]</p><p>Here is another price: [price EZWIDE], and then some more text.</p>"; 

$pattern = '/\[price (.*?)\]/'; 
preg_match_all($pattern, $text, $matches); 

foreach ($matches[1] as $key => $code) { 

$p = $db->get_row("SELECT prodPrice, prodName FROM products WHERE SKU ='$code' LIMIT 1"); 

$button = '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<p style="TEXT-ALIGN: left"> 
<input type="hidden" name="cmd" value="_cart" /> 
<input type="hidden" name="business" value="[email protected]" /> 
<input type="hidden" name="add" value="1" /> 
<input type="hidden" name="item_name" value="'.$p->prodName.'" /> 
<input type="hidden" name="item_number" value="'.$code.'" /> 
<input type="hidden" name="amount" value="'.$p->prodPrice.'" /> 
<input type="hidden" name="no_shipping" value="2" /> 
<input type="hidden" name="currency_code" value="USD" /> 
<input type="image" border="0" name="submit" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it\'s fast, free and secure!" /></p> 
     </form>'; 

$text = preg_replace_callback($pattern, $button, $text); 
} 

echo $text; 

什麼是導致錯誤信息?

+0

在這個設置中,因爲你已經在foreach循環中,所以你不想使用preg_replace_callback()。你只想使用preg_replace()。 preg_replace_callback()需要一個函數名稱(或一個匿名函數)作爲第二個參數,您將它視爲preg_replace()並傳遞替換的HTML。看看文檔http://php.net/manual/en/function.preg-replace-callback.php – mmiles

+0

你是對的,mmiles,謝謝。做了這個改變,它效果很好!我也有一個快樂的客戶。謝謝您的幫助!! –