0

I have this HTML content:

$test = '<section><div class="row">
        <div class="col-xs-12" data-type="container-content">
        <section data-type="component-photo"><div class="photo-panel">
        <img src="http://somewhere.com/XpV8lCLD6pChrysanthemum.jpg" width="100%" height="" style="display: inline-block;">
</div></section></div>
</div></section>';

And I have this code for str_replace so that it will add a closing tag for the img:

$new = str_replace('"></div>', '"/></div>', $test);

echo $new;

When check this out using inspect element the image is not closed as expected. What could I be doing wrong here?

Additional notes:

I need the closing tag for PHPWord to work. Also, The value of $test is from $_POST as this is fed through AJAX.

Thank you

magicianiam
  • 1,474
  • 7
  • 33
  • 70
  • Because there's a linebreak – u_mulder Jan 14 '18 at 12:41
  • I tried removing the line break but the content was not replaced – magicianiam Jan 14 '18 at 12:54
  • Inspect element (in chrome?) never shows the closing slash on img. It doesn't need to because img elements are implicit self closing. You never need to close the img tag with /> unless you want to use strict xhtml instead of the more common html language. The same counts for elements like hr, br, input and meta. – René Jan 14 '18 at 13:06
  • @René I'm using PHPWord and when an img tag misses a it's closing `/` it returns an error. – magicianiam Jan 14 '18 at 14:15
  • That's important information to add to your question. You do say something about "inspect element" (in unknown browser) but it's irrelevant to browsers to have it closed and with that in mind I thought this to be an irrelevant question to solve. – René Jan 14 '18 at 14:44
  • I updated the question. I'm still trying to find a way around this – magicianiam Jan 14 '18 at 15:02

2 Answers2

1

There is a newline between the tag and the closing tag. You should either reformat the $test variable or use:

$new = preg_replace('/>[\n\r]<\/div>/', '/></div>', $test);

You are however in dangerous territory here as all matches will be replaced. HTML is a language that does not play well with regex.

It's much better to tackle the problem before it takes html form if possible.

Ghlen
  • 659
  • 4
  • 14
  • A script is running that changes it for some reason when it is turned into HTML so the best thing I could here is manipulate it in php – magicianiam Jan 14 '18 at 12:55
  • Well in that case, this will do the job just fine for this simple string. – Ghlen Jan 14 '18 at 12:56
  • The expression doesn't match my string. I tested it on RegExr – magicianiam Jan 14 '18 at 15:15
  • It does when i test it... the pattern you need to enter between / and /g is: >[\n\r]<\/div>. When i run the php script it also works. – Ghlen Jan 14 '18 at 16:12
1

to do what you are looking for, you need to search for the text in that line (other answers also tell more directly - the issue is the line break.

$new = str_replace('ck;">\n</div><section>','ck;">\n</div></div><section>',$test);
Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28
  • The ending lines before `">` is dynamic so it can't be guessed properly all the time – magicianiam Jan 14 '18 at 12:55
  • could you be more specific? "> doesn't tell much - that is in there a lot. The point is that you have to find something that is 'testable' and use that. I'm adjusting my answer to what I think you are looking for, though guessing what you mean makes it hard to answer. – Apps-n-Add-Ons Jan 14 '18 at 13:00
  • Sorry but that's the most specific part of the content I found to be constant. I would have loved it if I can get it more specific as well. Thank you – magicianiam Jan 14 '18 at 13:01