删除 <tbody> php </tbody> 之间的所有内容



我整天都在研究这个问题,但无法弄清楚! 我有以下代码:

<?php echo $_product->getCustomProductInfo();?>
<?php if (!(trim($_product->gettempven()) =="" || trim($_product->gettempven()) =="None")){?>
<address style="position: absolute; right: 40px; top: 5px;">
<a class="aw-widget-legal" href="https://www.accuweather.com/en/id/semaya/<?php echo $this->htmlEscape($_product->getData('tempven'));?>/weather-forecast/<?php echo $this->htmlEscape($_product->getData('tempven'));?>">
<!--
By accessing and/or using this code snippet, you agree to AccuWeather’s terms and conditions (in English) which can be found at https://www.accuweather.com/en/free-weather-widgets/terms and AccuWeather’s Privacy Statement (in English) which can be found at https://www.accuweather.com/en/privacy.
-->
</a>
<div id="awcc1494497543780" class="aw-widget-current" style="width: 60px;" data-locationkey="<?php echo $this->htmlEscape($_product->getData('tempven'));?>" data-unit="c" data-language="en-us" data-useip="false" data-uid="awcc1494497543780">&nbsp;</div>
<script type="text/javascript" src="https://oap.accuweather.com/launch.js"></script>
</address>
<?php } ?>

这会从Mangento 1后端提取一些信息,此信息如下:

<table style="width: 900px; height: 156px;">
<tbody>
<tr>
<td><span style="color: #37454d;"><strong>Resort Name:</strong></span></td>
<td style="text-align: left;"><span style="color: #37454d;">explora Valle 
Sagrado, Peru</span></td>
</tr>
<tr>
<td><span style="color: #37454d;"><strong>Country Name:</strong></span></td>
<td><span style="color: #37454d; font-family: arial, helvetica, sans-serif; 
font-size: 13px; font-style: normal; font-variant-ligatures: normal; font- 
variant-caps: normal; font-weight: normal; letter-spacing: normal; text- 
align: left; text-indent: 0px; text-transform: none; white-space: normal; 
word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: 
#ffffff; text-decoration-style: initial; text-decoration-color: initial; 
float: none; display: inline !important;">Peru</span></td>
</tr>
<tr>
<td><span style="color: #37454d;"><strong>Resort Address:</strong></span> 
</td>
<td>
<div class="is-hidden-mobile blEntry address ui_link " data-popover="small" 
data-position="below" data-element=".content" data- 
options="closeOnMouseAway" data-maxwidth="300" data-mapfilters=""> 
<span>Unnamed Rd Urquillos, Peru</span></div>
</td>
</tr>
<tr>
<td><span style="color: #37454d;"><strong>Website:</strong></span></td>
<td>
<p><span style="color: #37454d;">https://www.explora.com/hotels-and- 
travesias/sacred-valley-peru/</span></p>
</td>
</tr>
<tr>
<td><span style="color: #37454d;"><strong>Phone:</strong></span></td>
<td><span style="color: #37454d;">+56 2 2395 2580</span></td>
</tr>
</tbody>
</table>
<p><iframe style="border:0" src="https://www.google.com/maps/embed? 
pb=!1m14!1m8!1m3!1d15528.573314149919!2d-72.0483157!3d- 
13.341359!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x1a052e519e34e771!
2sExplora+Valle+Sagrado!5e0!3m2!1sen!2sjp!4v1531275200849" height="450" 
width="600"></iframe></p>

我试图实现的是删除标签之间的所有内容,包括标签。

有没有办法做到这一点?

非常感谢您的帮助。

谢谢:)

使用正则表达式解析 html 不是推荐的做法,因为 html 标签可能具有多个级别的嵌套,这可能会给您带来意想不到的结果。

但以防万一,您将其作为快速黑客进行,并且不打算长期使用此解决方案,您可以使用此正则表达式。

(?s)<table.*?</table>

在这里演示, https://regex101.com/r/XsnZiC/1

这是你可以用来实现这一点的 php 代码,

$table_str="<h1>my header</h1><table aa=bb>dsfsfd</table><p>hello para</p>";
$table_str = preg_replace("/(?s)<table.*?</table>/", "", $table_str);
echo $table_str;

如果您遇到任何问题,请告诉我。

如果您确定只有一个<table>元素,那么您可以简单地匹配:

<table[sS]+?</table>

并替换为empty string.

正则表达式只是从匹配"<table"开始,然后继续将所有内容匹配到"</table>"。

替换后,您将在最后一个"<p>"中拥有内容。

最新更新