1

I need the str_replace line of code below to match only when inside a <td></td> where there will be other random stuff, too (so maybe wildcards are needed?). I assume there's some regex involved but I couldn't make it work. The code works, but I just want it to work in my tables vs. the entire page.

For reference, the larger snippet that includes this code breaks long product specs into two lines at the hyphen instead of weird places when word-wrap naturally occurs in the table.

Eg: (16 x 20 x 18 (406 x 508 x 457) - 24 x 36 x 18 (711 x 914 x 457)

$html = str_replace( ') - ', ') - <br />', $html );

It would be nice if it also worked even if the person entering all of our data doesn't use spaces around the hyphens for whatever reason or maybe accidentally does on one side or the other. I think there will always be parenthesis.

Thanks!

Zuggle
  • 49
  • 4
  • Exactly what _"other random stuff"_? Is your example the only format you'll see? –  Dec 09 '20 at 23:03
  • Let's start with the format in the example (and the very first parentheses after Eg: doesn't belong there, sorry). – Zuggle Dec 09 '20 at 23:31

1 Answers1

0

It sounds like you need preg_replace() rather than str_replace()

Try this:

<?php
$regex = "/^(<td.*?>)(.*?)\s*-\s*(.*)(<\/td>)$/i";
$html = '<td class="someClass">16 x 20 x 18 (406 x 508 x 457) - 24 x 36 x 18 (711 x 914 x 457)</td>';
$htmlOut = preg_replace($regex,'$1$2 - <br>$3$4', $html, -1, $replacements);

echo "Initial string: ".htmlentities($html)."<br>";
echo "Replacements: ".$replacements.'<br>';
echo "Output: ".htmlentities($htmlOut);

?>

Output:

Initial string: <td class="someClass">16 x 20 x 18 (406 x 508 x 457) - 24 x 36 x 18 (711 x 914 x 457)</td>
Replacements: 1
Output: <td class="someClass">16 x 20 x 18 (406 x 508 x 457) - <br>24 x 36 x 18 (711 x 914 x 457)</td>

The key here is capturing the portions from the original string and reassembling them later. The four capturing groups capture, in order, the leading <td> tag (including attributes), the text left of the hyphen, text right of the hyphen, the closing </td> tag. Note that the hyphen, together with arbitrary amounts of whitespace to either side is matched but not captured.

When reassembling just concatenate the various captured groups and insert whatever you want instead of the hyphen.

Note that this is a very specific use of regular expressions to parse a specific HTML tag. You shouldn't generally use regex to parse HTML. See here for what can happen.