Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Way to Escape Quotes in HTML string that includes PHP variables

I'm writing a string of HTML that needs to take in some of my PHP variables. However, I can't seem to escape the double quotes correctly.

Attempt 1:

$html .= '<span class="badge"><a href="#" style="color:orange"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction(\''.$configType.'\')"></span></a></span>';

Result:

<span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction(\' project\')"=""></span>

Attempt 2:

$html .= '<span class="badge"><a href="#" style="color:orange"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction('.'$configType'.')"></span></a></span>';

Result:

<span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction(project)"></span>

Close, but should be 'project'.


Desired Result:

<span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction('project')"></span>
like image 247
Sara Fuerst Avatar asked Jan 23 '26 16:01

Sara Fuerst


1 Answers

Here it comes, you were just one step closer with first try, you just need to move double quotes out of single ones.

$html .= '<span class="badge"><a href="#" style="color:orange"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true" onclick="sendToProduction("'.$configType.'")"></span></a></span>';

Here you can see a live sample

like image 97
Fabio Avatar answered Jan 25 '26 05:01

Fabio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!