【问题标题】:php preg_match_all and preg_replacephp preg_match_all 和 preg_replace
【发布时间】:2013-07-17 12:16:06
【问题描述】:

我想将所有输入值编码为 base64 编码。所以我需要一些帮助...我的代码是:

$check_hash = preg_match_all('/<input type="text" value="(.*?)">/mis', $ays, $hashtweet);

$ays = preg_replace('/<input type="text" value="(.*?)">/', base64_encode($hashtweet[1]), $ays);

echo $ays;

我的页面在这里: http://www.ceay.biz/test/vkmp3/

但它并没有给我我想要的东西。谁能帮帮我?

【问题讨论】:

  • 你认为正确的值是多少?
  • 我很愚蠢 :) 我只是将所有输入值更改为编码值

标签: php preg-replace preg-match str-replace preg-match-all


【解决方案1】:

使用 preg_replace_callback 来做这个(需要 php 5.3 来关闭)

$ays = preg_replace_callback('/value="(.*)"/', function ($match) {
                          return "value=\"".base64_encode($match[1])."\""; }, $ays);

适用于 php 5.3 之前的环境

if (!function_exists("valueReplacer")){
    function valueReplacer ($m){
        return "value=\"".base64_encode($m[1])."\""; 
    }
}
$ays = preg_replace_callback('/value="(.*)"/', "valueReplacer", $ays);

【讨论】:

  • 解析错误:语法错误,第 65 行 index.php 中出现意外的 T_FUNCTION
  • 闭包语法仅适用于 php 5.3+ ...我将更新显示非闭包用法
【解决方案2】:

您需要调用 preg_replace_callback 来执行 PHP 代码作为替换:

$ays = preg_replace_callback('/<input type="text" value="(.*?)">/', function ($m) {
                             return base64_encode($hashtweet[1]); }, $ays);

【讨论】:

  • 是的,请提供$hashtweet$ays 的示例值
  • 如果您可以提供$hashtweet$ays 的示例值,那将非常有帮助。
  • hashtweet 是一个数组。这是 hashtweet 数组 ceay.biz/test/vkmp3
  • 你想base64_encode $hashtweet 数组中的所有元素吗?老实说,我无法完全理解您的问题。
  • 是的,我想base64_encode $hashtweet 数组中的所有元素
【解决方案3】:

你可以检查一下:

$string = '<input type="text" value="http://cs9-10v4.vk.me/p1/63ec3a36b2eb1c.mp3">';
preg_match('/<input type="text" value="(.*)">/', $string, $matches);
$string = preg_replace('/(<input type="text" value=").*(">)/', "$1".base64_encode($matches[1])."$2", $string);
var_dump($string);

【讨论】:

  • 好的,那么您需要对您拥有的所有值进行 foreach。你是如何获得输入的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多