Apostroph within echo

4

Good, I'm trying to put the pictures on the slide, but the apostrophe is breaking, where is the error? edited below the summary after the tests that I did even using the \.

meu codigo atual
echo "<div class='fill' style='background-image:url(\'img/{$exibe[0]->idnoticia}/{$exibe[0]->imagem}\');'></div>";


codigo de exemplo do bootstrap
<div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide One');"></div>


codigo exibido pelo browser
<div class="item active"><div class="fill" style="background-image:url(\" img="" 22="" 1429033737_screenshot_11.jpg\');'=""></div>

I really do not understand what happens if someone can give a light so remembering that this way works, but would like to know the above error

<div class="fill" style="background-image:url('img/<?php echo "{$exibe[0]->idnoticia}/{$exibe[0]->imagem}"; ?>');"></div>
    
asked by anonymous 15.04.2015 / 01:33

1 answer

8

The problem is that you are using apostroph (') for the attributes of your div and also for things inside the attribute, and escapes without necessity, in that the browser tries to interpret some of its output and understands this mess you posted.

Your current output should look something like this:

<div class='fill' style='background-image:url(\'img/22/1429033737_screenshot_11.jpg\');'>

As this html you generated is invalid, the browser tries to interpret and gets that soup you posted in the third example.

I think this solves your problem:

<?php
echo "<div class=\"fill\" style=\"background-image:url('img/{$exibe[0]->idnoticia}/{$exibe[0]->imagem}')\"></div>";

If you did not want to get lost in escapes, you could use the heredoc syntax

example:

<?php
echo <<<HTML
<div class="fill" style="background-image:url('img/{$exibe[0]->idnoticia}/{$exibe[0]->imagem}')"></div>
HTML;
    
15.04.2015 / 04:13