English 中文(简体)
预想 - 清除一些不想要的碎片
原标题:preg_replace to remove some unwanted divs

In the following code, I need to remove <div class="grid_8"></div>. They will always be at the start and finish of my string, for example:

<div class="grid_8"><img src="http://rps.sanscode.com/site/assets/media/images/rps_mini_logo.png" border="0" alt="Rapid Print Solutions" style="margin-bottom: 30px;" /> 
<h1></h1> 
</div>

什么是消除这一疾病的适当条例? 8人可在1至16岁之间。

增 编

son


@Amjad

我的法典

public function fix_grid(){
     $result = db::query("select * from sc_content_components where component_value_1 like  %grid_% ")->fetchAll(PDO::FETCH_ASSOC);

     foreach($result as $item){
  $json = json_decode($item[ component_value_1 ]);
  if(is_null($json)) continue;
  $x = reset($json);

  echo htmlspecialchars($x);
  echo "<p>=======================<b>Changes to: </b></p>";

  $patterns = array( /^<(div)((?:s+w+(?:s*=s*(?:(?:"[^"]*")|(?: [^ ]* )|[^>s]+))?)*)s*(/?)>/ 
                  ,  /</(div+)[^>]*>$/ );
  $x = preg_replace($patterns, array(  ,  ), trim($x));
  echo htmlspecialchars($x);
  echo "<hr>";


  $json[0]=$x;
//  $ne[ component_value_1 ] = json_encode($json);
//  db::where( component_id , $item[ component_id ]);
//  db::update( sc_content_component , $ne);
     }

 }

I m 采用以下分类法(@Amjad Masad),它不去掉最后一部分。

你可以看到,我正在使用三米,看来没有工作。

最佳回答

开幕

$patterns = array( /^<(div)((?:s+w+(?:s*=s*(?:(?:"[^"]*")|(?: [^ ]* )|[^>s]+))?)*)s*(/?)>/ 
                  ,  /</div[^>]*>$/ );
preg_replace($patterns, array(  ,  );, trim($htmlString));
问题回答

对于所述问题,解决办法是:

<<>Edit:经扩大后,>

/^s*<divs (?:".*?"| .*? |[^>]*?)*
            (?<=s)classs*=s*([" ])s*grid_(?:1[0-6]|[1-9])s*1
            (?:".*?"| .*? |[^>]*?)*
     >
     (.*)
     </divs*>
s*$/xs

replacement = "$2"

更简单一些。

如果1至16>重要,范围更大......>

$match = preg_replace("/^<([^>]*)grid_(1[0-6]|[1-9])([^>d]*)>(.+)<([^>]*)>$/s","$4",trim($str),-1,$hmany);
if($hmany){
    echo "$match <br>";
}else{ echo "No match found! <br>"; }

如果1至16 是唯一的可能范围,因此不相干。

$match = preg_replace("/^<([^>]*)>(.+)<([^>]*)>$/s","$2",trim($str),-1,$hmany);
if($hmany){
    echo "$match <br>";
}else{ echo "No match found! <br>"; }

在这方面。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签