How to display quotation marks in echo in PHP?

6

I'm developing an application that uses PHP + AJAX and I came across a boring problem. AJAX only recognizes the function if it is like this:

onclick="remover_musica_ftp('0','Deposito de bebida'
);"

I'm using PHP to "print" these values in the browser like this:

echo "<a href='#excluir' class='button icon-trash' onclick=remover_musica_ftp('0','".$nomes."');  title='Excluir'></a>";

I have to use php because I'm doing foreach . So far so good, the problem is that when the word contains spaces, ajax simply does not respond, because the code would need to be as in the first example, but when I add the " tag automatically PHP becomes invalid, since the even echo started with a quotation mark. Does anyone know how to proceed in this case? If I open the echo with a ' it will give error in the same way, since I would need to invert the onclick of ajax that would not allow ' in the functions. Give me a suggestion!

    
asked by anonymous 16.08.2014 / 09:46

2 answers

4

You need to escape " .

$nomes = 'nomes...';
echo "<a href=\"#excluir\" class=\"button icon-trash\" onclick=remover_musica_ftp( 0 , \"$nomes\" ); title=\"Excluir\">link</a>";

Output

<a href="#excluir" class="button icon-trash" onclick=remover_musica_ftp( 0 , "nomes..." ); title="Excluir">link</a>


If the argument preceding the name is numeric, you do not need to use '

remover_musica_ftp( integer , "string" )
    
16.08.2014 / 10:03
6

When you want to show the same type of quotation marks you are using to lock the string, you need to escape it using a \

$string = 'aspas simples: \' e aspas dupla: " ';
$string = "aspas simples: ' e aspas dupla: \" ";

A situation that may occur is that you have to escape a quotation mark within an already escaped string, so you'll need to put an escaped bar, like in ex. below:

echo '<a onclick="remover_musica_ftp(0,\'It\\'s Name Is\');"></a>';

// retorno:
<a onclick="remover_musica_ftp(0,'It\'s Name Is');"></a>
    
16.08.2014 / 14:03