簡體   English   中英

替換函數WP中的字符串

[英]Replacing string in function WP

我想替換我正在使用的插件中的部分字符串插入一個貝寶按鈕。

此字符串在此函數中。 我已經評論了很多,並顯示了我想要過濾的部分:

add_shortcode('paypal', 'paypal_options');
function paypal_options($atts) {
  $atts = shortcode_atts(array('name' => 'Example Name','price' => '0.00','size' => '','align' => ''), $atts);
  $options = get_option('paypal_settingsoptions');
  foreach ($options as $k => $v ) { $value[$k] = $v; }

  $output = "";
//commented out excess code
  $output .= "<div $alignment>";
  $output .= "<form target='$target' action='https://www.$path.com/cgi-bin/webscr' method='post'>";
//commented out excess code
  $output .= "<input style='border: none;' class='paypalbuttonimage' type='image' src='$img' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>";

  return $output;
}

我想替換這個特定的字符串:

<input style='border: none;' class='paypalbuttonimage' type='image' src='$img' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>

有了這個:

<input style='border: none;' class='paypalbuttonimage' type='submit' value='Buy Now' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>

將其從input type='image'更改為input type='submit'

我正在嘗試這個,但它似乎沒有抓住運營商:

function replace_ppbutton($output) {
  $og = "<input style='border: none;' class='paypalbuttonimage' type='image' src='$img' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>";
  $rep = "<input style='border: none;' class='paypalbuttonimage' type='submit' value='Buy Now' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>";
  return str_replace($og, $rep, $output);
}
add_filter('paypal_options', 'replace_ppbutton');

老問題我知道,但我認為用字符串替換來改變標記可能比使用正則表達式更脆弱!

嘗試使用帶有XPath查詢DOM解析器 ; 這樣的事情:

<?php
function replace_ppbutton($output) {
    $dom = new DomDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($output);
    $xpath = new DomXPath($dom);
    $button = $xpath->query("//input[@class='paypalbuttonimage' and @type='image']")->item(0);
    $button->setAttribute("type", "submit");
    $button->removeAttribute("src");
    return $dom->saveHTML();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM