用PHP代码替换数据库中的花括号数据


replace curly backets data from databse with php code

我将这些字符串存储在变量中:

    // This is the decoded text from your database:
 $dbStrings = '
<div class="container"> 
 <div class="row">        
  <div class="col-xs-12 col-sm-7 col-md-7 col-lg-7 Headerlogo">
    <a href="index.html">
        {{ type=Img, value=logo1.png, class=logo, alt=studio }}
    </a>
  </div>
 <!-- Edited header Starts -->    
<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5 headerSocial hidden-xs">  
    {{ type=Img, value=logo2.png, class=logo, alt=studio }}
    <div class="socialfollow">
    {{ type=Img, value=logo3.png, class=logo, alt=studio }}
        <a href="#" class="facebook"><i class="icomoon-facebook Three-Dee"></i></a> 
        <a href="#" class="dribble"><i class="icomoon-dribbble Three-Dee"></i></a> 
        <a href="#" class="twitter"><i class="icomoon-twitter Three-Dee"></i></a> 
        <a href="#" class="google"><i class="icomoon-google Three-Dee"></i></a> 
        <a href="#" class="linkedin"><i class="icomoon-linkedin Three-Dee"></i></a>
    </div>  
</div>   
 <!-- Edited header Ends -->  
</div>
</div>';

我用

处理它
 // First find the special code by finding any instance of {{ data1=data1, data2=data2 }} from the strings
    preg_match_all("/{{ (.*?) }}/", $dbStrings, $matches);
    //if found Note $matches[0]s are the curly brackets to be replaced according to the keys of $matches[1] (that is every instance of {{ data1=data1, data2=data2 }} )
    // $matches[1]s are the data strings to get the arguments from (that is $matches[0]s without the curly brackets i.e data1=data1, data2=data2)
    // use foreach so that each of $matches[1] will have their key
    foreach ($matches[1] AS $key => $widgets)
    {
        // Explode the value of each of $matches[1] (which are now $widgets with key) with comma to have params
        $params = explode(',', $widgets);
        // use for each on $params so that each param inside the widgets will be able to  have its key and value
        foreach ($params AS $param)
        {
            // Assign the explosion of each param with =
            $attr = explode('=', trim($param));
            // create a variable array called $arg which has its key as each of the attr[0] and its value as each of the corresponding attr[1]
            $arg[$attr[0]] = $attr[1];
        }
         // Now lets get to the replacement
        // if the type is image
       if(strtoupper(trim($arg['type'])) == 'IMG'){
          $value        = isset($arg['value'])  ?   $arg['value']   : "";
          $id           = isset($arg['id'])     ?   $arg['id']      : "";
          $class        = isset($arg['class'])  ?   $arg['class']   : "";
          $width        = isset($arg['width'])  ?   $arg['width']   : "";
          $height       = isset($arg['height']) ?   $arg['height']  : "";
          $alt          = isset($arg['alt'])    ?   $arg['alt']     : "";
          $title        = isset($arg['title'])  ?   $arg['title']   : "";
          $align        = isset($arg['align'])  ?   $arg['align']   : "";
          // Get the arrays of parameters according to Helper::addImg
          $array = array('id'=>$id,'class'=>$class,'width'=>$width, 'height'=>$height, 'alt'=>$alt,'title'=>$title,'align'=>$align);
          // Then replace the above code with the values
          echo str_replace($matches[0][$key], Helper::addImg($value, $array), $dbStrings);
       }
    }
    echo $dbStrings;

基本上我想要的是用

替换每个花括号的实例(我不知道里面的参数值)
Helper::addImg("value", array('class'=>'class','alt'=>'alt'));

现在它只替换了一个花括号请帮助由于

preg_replace_callback:

<?php
class Helper {
    function   addImg($value,$array) {
        return '__FAKE__'.var_export($value,true).'|'.var_export($array,true).'__FAKE__';
    }
}
// This is the decoded text from your database:
$dbStrings = '
<div class="container">
 <div class="row">
  <div class="col-xs-12 col-sm-7 col-md-7 col-lg-7 Headerlogo">
    <a href="index.html">
        {{ type=Img, value=logo1.png, class=logo, alt=studio }}
    </a>
  </div>
 <!-- Edited header Starts -->
<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5 headerSocial hidden-xs">
    {{ type=Img, value=logo2.png, class=logo, alt=studio }}
    <div class="socialfollow">
    {{ type=Img, value=logo3.png, class=logo, alt=studio }}
        <a href="#" class="facebook"><i class="icomoon-facebook Three-Dee"></i></a>
        <a href="#" class="dribble"><i class="icomoon-dribbble Three-Dee"></i></a>
        <a href="#" class="twitter"><i class="icomoon-twitter Three-Dee"></i></a>
        <a href="#" class="google"><i class="icomoon-google Three-Dee"></i></a>
        <a href="#" class="linkedin"><i class="icomoon-linkedin Three-Dee"></i></a>
    </div>
</div>
 <!-- Edited header Ends -->
</div>
</div>';
$result = preg_replace_callback("/{{ (.*?) }}/",function ($match) {
    var_export($match);
    $params = explode(',', $match[1]);

    $arg = [];
    // use for each on $params so that each param inside the widgets will be able to  have its key and value
    foreach ($params AS $param)
    {
        // Assign the explosion of each param with =
        $attr = explode('=', trim($param));
        // create a variable array called $arg which has its key as each of the attr[0] and its value as each of the corresponding attr[1]
        $arg[$attr[0]] = $attr[1];
    }
    // Now lets get to the replacement
    // if the type is image
    if(strtoupper(trim($arg['type'])) == 'IMG'){
        $value        = isset($arg['value'])  ?   $arg['value']   : "";
        $id           = isset($arg['id'])     ?   $arg['id']      : "";
        $class        = isset($arg['class'])  ?   $arg['class']   : "";
        $width        = isset($arg['width'])  ?   $arg['width']   : "";
        $height       = isset($arg['height']) ?   $arg['height']  : "";
        $alt          = isset($arg['alt'])    ?   $arg['alt']     : "";
        $title        = isset($arg['title'])  ?   $arg['title']   : "";
        $align        = isset($arg['align'])  ?   $arg['align']   : "";
        // Get the arrays of parameters according to Helper::addImg
        $array = array('id'=>$id,'class'=>$class,'width'=>$width, 'height'=>$height, 'alt'=>$alt,'title'=>$title,'align'=>$align);
        // Then replace the above code with the values
        return Helper::addImg($value, $array);
    }
    else {
        return '-Unhandled-:'.$match[0].'-Unhandled-';
    }
},$dbStrings);
echo '------------------- Output ----------------------';
echo $result;