Asked  7 Months ago    Answers:  5   Viewed   43 times

I have he following scenario:

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

Here is a reduced example:

<td>Heading 1</td>
<td>heading 2</td>
<td>Value 1</td>
<td>Value 2</td>

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?




Assuming <PRODUCT_LIST> tags will never be nested

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

//HTML array in $matches[1]
Wednesday, March 31, 2021
answered 7 Months ago

This will work only for non-nested parentheses:

    $regex = <<<HERE
    /  "  ( (?:[^"\\]++|\\.)*+ ) "
     | '  ( (?:[^'\\]++|\\.)*+ ) '
     | ( ( [^)]*                  ) )
     | [s,]+

    $tags = preg_split($regex, $str, -1,
                       | PREG_SPLIT_DELIM_CAPTURE);

The ++ and *+ will consume as much as they can and give nothing back for backtracking. This technique is described in perlre(1) as the most efficient way to do this kind of matching.

Wednesday, March 31, 2021
answered 7 Months ago

I didn't test the logic used in this page - But I can confirm the logical point made at the top of the page in big bold letters that says you shouldn't do what you're trying to do with regex.

Html is not uniform and edge cases will always bite you in the rear if you use regular expressions to handle the content of those tags in any real world situation. So unless your markup is extremely simplistic, uniform, 100% accurate, only contains html (not css, javascript or garbage) then your best bet is a dom parser library.

And really many dom parser libraries have problems too but you'll be miles ahead of the regex counterparts. The best way to get the text contet of tags is to render the html in a browser and access the innerText property of the given dom node (or have a human copy and paste the contents out manually) - but that isn't always an option :D

Saturday, May 29, 2021
answered 5 Months ago


return preg_replace('/(?<!-)b('.implode('|',$commonWords).')b(?!-)/i','',$input);

This adds negative lookaround expressions to the start and end of the regex so that a match is only allowed if there is no dash before or after the match.

Saturday, May 29, 2021
answered 5 Months ago

I think you're searching for something like this

/ism", $search, $match);

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

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

$text = preg_replace_callback("/
/ism", function($match) { return str_replace(" ", "&nbsp;", $match[1]); }, $search);
Saturday, July 3, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :