Apostrophe within echo

Asked

Viewed 319 times

4

well I’m trying to put on the slide the images but the bet is breaking , where is the error? edited below the summary after the tests 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 don’t understand what’s going on if someone can shed a light so just remembering that this way works, however I would like to know what the error above

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

1 answer

8


The problem is that you are using apostropho(') for the attributes of your div and also for things within the attribute, and escapes needlessly, in which the browser tries to interpret something of your output and understands this mess you posted.

Your current output must be something like this:

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

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

I think that solves your problem:

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

If you didn’t want to get lost in the escapes, you can use the syntax heredoc

example:

<?php
echo <<<HTML
<div class="fill" style="background-image:url('img/{$exibe[0]->idnoticia}/{$exibe[0]->imagem}')"></div>
HTML;
  • was unaware of this heredoc syntax, I tested it here and it worked, how much its solution to my problem had also tried also not worked

  • I’ll test it later, there must be some stupid mistake.

  • I think I’ve solved.

  • can change the code or post Aki in the comments what you did?

  • I changed the answer,.

  • 1

    follow example http://sandbox.onlinephpfunctions.com/code/1256f8257f5ba5bc574817d95b86f8b0156b8b87

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.