用PHP代码替换databse中的卷曲backets数据

时间:2016-09-09 12:18:51

标签: php

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

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

现在它只替换了许多花括号中的一个 请帮忙 感谢

1 个答案:

答案 0 :(得分:0)

使用<?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;

@receiver(pre_save, sender=JuicerBaseSettings)
def check_no_conflicting_juicer(sender, instance, *args, **kwargs):
    # If another JuicerBaseSettings object exists a ValidationError will be raised
    if JuicerBaseSettings.objects.exclude(pk=instance.pk).exists():
        raise ValidationError('A JuiceBaseSettings object already exists')