php - how to add slashes to only double quotes -
i want add slashes double quotes html elements how doing , not working me :
$str =" <table class="body-wrap" style="font-family: 'helvetica neue', 'helvetica', helvetica, arial, sans-serif; width: 100%; margin: 0; padding: 0;"> <tr style="font-family: 'helvetica neue', 'helvetica', helvetica, arial, sans-serif; margin: 0; padding: 0;"> <td style="font-family: 'helvetica neue', 'helvetica', helvetica, arial, sans-serif; margin: 0; padding: 0;">this test</td></tr></table> ";
this php function :
$str2 = addcslashes($str, '"');
i think have problems @ beginning of string , dont know how declare :
$str =" or $str = '
both me didnt work
delete string $str =" or $str = '
entirely wrong.
1) use css style sheet store css outside html , avoid repetition.
2) use single quotes in html syntax (and php string in double quotes) avoid need escape html @ (javascript elements exception, however).
example of points 1 , 2 combined:
css file
.body_wrap_table { width: 100%; } .table_td_tr { font-family: 'helvetica neue', 'helvetica', helvetica, arial, sans-serif; margin: 0; padding: 0; }
html php string
$str = " <table class='body-wrap body_wrap_table'> <tr class='table_td_tr'> <td class='table_td_tr'>this test</td></tr></table> ";
so no need quote escaping @ all.
3) if want can use regex function such preg_replace
exampled here:
$str = preg_replace('/"+(?<!\\")/', '\"', $str);
this regex match "
character not preceeded \
character, , replace escaped quote.
4) possibly simpler approach may or may not solution (and less processor heavy regex) use str_replace such :
$str = str_replace("\"","\\\",$str);
this replaces text fitting shape "
\"
may cause double escaping if quote has escape mark preceeding it.
references:
Comments
Post a Comment