1

I have been working on this all day and cannot figure this out! I have the following code:

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

This pulls some information from a Mangento 1 back end, this information is below:

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

What I am trying to achieve is to remove all the content between and including the tags.

Is there a way I can do this?

Your help is greatly appreciated in advance.

Thanks :)

Lee
  • 11
  • 1
  • Obligatory ["Do NOT use regex for html"](https://stackoverflow.com/a/1732454/7416863) You should look into actual html parsers for php – jjspace Oct 23 '18 at 17:51
  • Why not just select the `iframe` with a parser? Removing the data seems like it'll be inaccurate. – user3783243 Oct 23 '18 at 17:52
  • Have you tried using DOMDocument? This example shows how to find elements within a document. http://php.net/manual/en/class.domdocument.php#95894 Then you would delete it, like https://stackoverflow.com/a/15272752/6031948 – Brian Wagner Oct 23 '18 at 17:53
  • If i'm honest the only section I want to keep of the code is the bottom section between

    and

    . But the concern is there are inconsistencies within the code and the only consistent way to get the correct result is the remove all the content between and .
    – Lee Oct 23 '18 at 17:54

2 Answers2

1

Parsing html with regex is not a recommended thing to do because html tags may have multiple levels of nesting and that may give you unexpected results.

But just in case, you are doing it as a quick hack, and don't intend to use this solution in long term, you can use this regex.

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

Demo here, https://regex101.com/r/XsnZiC/1

This is the kind of php code you can use to achieve this,

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

Let me know if you face any issues.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • I see this works great at selecting the table but what do I do next to remove it/replace with an empty string? Thanks again for your help. – Lee Oct 24 '18 at 08:19
  • I have added the sample code to demonstrate how you need to write code. Hope it helps – Pushpesh Kumar Rajwanshi Oct 24 '18 at 08:29
  • Im a little confused where to place this within the first block of code on my question? – Lee Oct 24 '18 at 08:48
  • You can insert these two lines wherever you have your table html string. Let's say you have your table html in $table_str variable. Then the preg_replace line in my code removes table tag data and returns back the remaining string and assigns back to $table_str variable. So it is just one line of code that is doing all. – Pushpesh Kumar Rajwanshi Oct 24 '18 at 08:54
  • You're probably going to call be dumb but would to be able to demonstrate where to place this in the code I have in the first block please? Thanks again :) – Lee Oct 24 '18 at 09:00
0

If you're sure there's only one \<table\> element, then you can simply match:

\<table[\s\S]+?\</table\>

and replace with an empty string.

The regex simply starts by matching '<table', then continues to match everything up to '</table>'.

After replace you will have the content in the last '<p>'.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57