preg_replace 删除一些不需要的 div

发布于 2024-10-12 22:51:41 字数 1435 浏览 6 评论 0原文

在下面的代码中,我需要删除

. 它们将始终位于字符串的开头和结尾,例如:

<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>

What is a合适的正则表达式让 preg_replace 删除它? 8 可以是 1 到 16 之间的任何数字。

谢谢

Jason


@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);
     }

 }

我正在使用下面的正则表达式 (@Amjad Masad),它不会删除最后一个 div。

正如你所看到的,我正在使用修剪,但它似乎不起作用

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>

What is a suitable regex for preg_replace to remove it? 8 can be any number between 1 and 16.

Thanks

Jason


@Amjad...

Here is my code

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 using the regex below (@Amjad Masad) and it doesn't remove the last div.

As you can see I am using trim and it doesn't seem to work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝影如岚 2024-10-19 22:51:41

对于所述问题,解决方案如下:

编辑:扩展正则表达式 =

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

replacement = "$2"

For the stated problem, this is the solution:

Edit: expanded regex =

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

replacement = "$2"

℡寂寞咖啡 2024-10-19 22:51:41

对于开放

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

For the open

$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
                  , '/<\/div[^>]*>$/');
preg_replace($patterns, array('','');, trim($htmlString));
Smile简单爱 2024-10-19 22:51:41

简单一些。

如果 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>"; }

此致。

Somewhat simpler.

If the 1 to 16 is significant and part of a greater range...

$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>"; }

If the 1 to 16 is the only possible range and therefore irrelevant...

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

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文