0

I have a Json file with html content in some rows, how ever everytime I try to use preg_replace to match this combination of a break and new line it doesn't work.

For example, <br> No atraviesa el papel

Want to replace with: <br>No atraviesa el papel

Already tried:

$pattern = '/<br>\n/m';
$subs = '<br>';
$json_response = preg_replace($pattern, $subs, $json_response);

Im looking to remove the newline and leave only the <br>

stu
  • 9
  • 2

2 Answers2

0

You need to start with a positive lookahead.
And then substitute the match with an empty string.

/(?<=<br>)\s*\R/

Demo on regex101

  • edit: added a match for white space, if there is any after the <br>
  • edit: made it more concise and universal with the help of @ user3783243
Aleksandar
  • 1,496
  • 1
  • 18
  • 35
0

Is it something you are looking for?

<?php

$json_response="<br>
No atraviesa el papel
";

$tobe = "<br>No atraviesa el papel
";

$pattern = '/<br>([\r\n]+)/is';
$subs = "<br>";
$json_response = preg_replace($pattern, $subs, $json_response);

echo $json_response;
echo "\r\n", $json_response==$tobe?"Matched":"Did not match";

It outputs: Matched.

Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41