Viewed   101 times

I have he following scenario:

Got an HTML template file that will be used for mailing.

Here is a reduced example:

    <table>
<tr>
<td>Heading 1</td>
<td>heading 2</td>
</tr>
<PRODUCT_LIST>
<tr>
<td>Value 1</td>
<td>Value 2</td>
</tr>
</PRODUCT_LIST>
</table>

All I need to do is to get the HTML code inside <PRODUCT_LIST> and then repeat that code as many times as products I have on an array.

What would be the right PHP Regex code for getting/replacing this List?

Thanks!

 Answers

1

Assuming <PRODUCT_LIST> tags will never be nested

preg_match_all('/<PRODUCT_LIST>(.*?)</PRODUCT_LIST>/s', $html, $matches);

//HTML array in $matches[1]
print_r($matches[1]);
Thursday, November 10, 2022
5

The Arabic regex is:

[u0600-u06FF]

Actually, ?-? is a subset of this Arabic range, so I think you can remove them from the pattern.

So, in JS it will be

/^[a-z0-9+,()/'su0600-u06FF-]+$/i

See regex demo

Tuesday, October 11, 2022
1

I think you're searching for something like this

<?php
preg_match_all("/
Code:
(.*?)
/ism", $search, $match);

hover, I'd suggest you to use BBcode parsers instead


To replace all spaces with &nbsp;, simply use preg_replace_callback

<?php
$text = preg_replace_callback("/
Code:
(.*?)
/ism", function($match) { return str_replace(" ", "&nbsp;", $match[1]); }, $search);
Wednesday, August 24, 2022
3

For this PHP regex:

$str = preg_replace ( '{(.)1+}', '$1', $str );
$str = preg_replace ( '{[ '-_()]}', '', $str )

In Java:

str = str.replaceAll("(.)\1+", "$1");
str = str.replaceAll("[ '-_\(\)]", "");

I suggest you to provide your input and expected output then you will get better answers on how it can be done in PHP and/or Java.

Sunday, October 9, 2022
 
haodong
 
4

You can use lazy quantifiers instead.

$s="foo<div>Some content is <b>bold</b>.</div>barn";

print preg_replace("/<div>.+?</div>/i", "", $s);'

output:

foobar

UPDATE per comments:

[ghoti@pc ~]$ cat doit.php 
<?php

$text = 'text text text s html tagove
<div id="content"><b> stfu </b> ss adsda sdsa </div>
oshte text s html tagove';

print preg_replace('/<div id="content">.+?</div>/im', '', $text) .  "n";

[ghoti@pc ~]$ php doit.php 
text text text s html tagove

oshte text s html tagove
[ghoti@pc ~]$ 
Saturday, August 13, 2022
 
andlrc
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :