简体   繁体   English

替换函数WP中的字符串

[英]Replacing string in function WP

I would like to replace part of a string in a plugin I am using that inserts a paypal button. 我想替换我正在使用的插件中的部分字符串插入一个贝宝按钮。

This string is in this function. 此字符串在此函数中。 I have commented out a lot of the excess and showing which parts I would like to filter: 我已经评论了很多,并显示了我想要过滤的部分:

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

I want to replace this specific string: 我想替换这个特定的字符串:

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

with this: 有了这个:

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

Changing it from an input type='image' to input type='submit' 将其从input type='image'更改为input type='submit'

I am trying this, but it doesn't seem to catch the operator: 我正在尝试这个,但它似乎没有抓住运营商:

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');

Old question I know, but I think altering markup with string replacement is probably even more fragile than using regular expressions ! 老问题我知道,但我认为用字符串替换来改变标记可能比使用正则表达式更脆弱!

Try using a DOM parser with an XPath query instead; 尝试使用带有XPath查询DOM解析器 ; something like this: 这样的事情:

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