php preg_match_all和preg_replace

时间:2013-07-18 13:48:00

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

我想将所有输入值编码为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/

但它不会给我我想要的东西。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

使用preg_replace_callback执行此操作(需要php 5.3进行关闭)

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

for pre php 5.3 environment

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

您可以查看:

$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);
相关问题