-1

I need to escape a string that has a lot of " and '. The string is a binary from a database. I have this:

$String64 = '<a data-String64="'.$binary64.'">Link</a>';

So the $binary64 has some text like target:"29", width=100'height=200...

So my problem is that the "' are closing the data-String64 before it can output the entire variable. I have tried addslashs(), but nothing good.

How can I escape this?

This is what I have tried:

$String64 = "<a data-string64='${binary64}'><b><i class='fa fa-eye'></i></b></a>";
$String64 = "<a data-string64='{$binary64}'><b><i class='fa fa-eye'></i></b></a>";
$String64 = "<a data-string64=\'${binary64}\'><b><i class='fa fa-eye'></i></b></a>";
$String64 = "<a data-string64=\'$binary64\'><b><i class='fa fa-eye'></i></b></a>";
$String64 = "<a data-string64=\"$binary64\"><b><i class=\"fa fa-eye\"></i></b></a>";
Dharman
  • 30,962
  • 25
  • 85
  • 135
Alexis Garcia
  • 452
  • 3
  • 15
  • Do you really need those `'` and `"`s any problem if you remove them ? – Vinay Mar 24 '20 at 16:16
  • like i said, it is a binary from my DB. So i need it because the binary is a PDF file that ill render in an iframe. – Alexis Garcia Mar 24 '20 at 16:19
  • Heh, blob file works in other way. – Aksen P Mar 24 '20 at 16:22
  • lol i cant var_dump all of it, its a binary of 40kb. i can post some part v $ � � &���� v $ ��������cO��� ��W��@N����#Iq�Ie�_��c�Ժ��B|Γ����f��ƐF�/9��y�Ҙ���z�)�L�Tk��%�T��0SJ��#���N��0rl���N�L�%�Ƿc�.��y�=�^n����0�����Ù Mp�{o�;���_�wf.�%^�x���dK\�*N��d�I�Tm)B�MM:[oa,C�6aFVrY��O�&�%@DL>6�Q����u��j*����Ot��B�T� – Alexis Garcia Mar 24 '20 at 16:23
  • I see , as long as you use it like binary it should be fine (like write to a pdf file) why you want to display it as text? – Vinay Mar 24 '20 at 16:35
  • i needed that in a data- to get it by JS, but i guess ill do it by ajax and forget this method – Alexis Garcia Mar 24 '20 at 16:36
  • Yes I've done that in ajax just make the request as binary and browser will treat it as such. Things is HTML is supposed to be a TEXT-only document therefore can't handle the binary content that's the reason we use EXTERNAL Font files to render custom fonts/emojis – Vinay Mar 24 '20 at 16:38

1 Answers1

0

You need to make next:

$String64 = '<a data-String64="'.base64_encode( $binary64 ).'">Link</a>';

or

$String64 = '<a data-String64="'. base64_encode($binary64->load()) .'">Link</a>';

Documentation of base64_encode()

For your tag try:

echo '<embed 
      style="position:absolute; height:100%; width:100%;" 
      type="application/pdf" 
      src="data:application/pdf;base64,' . base64_encode($binary64->load()) . '"></embed>';
Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • lol thats not it. you did a simple variable ````$var = ''TTTT;```` the problem is that i have multiple " and ' inside my var – Alexis Garcia Mar 24 '20 at 16:14
  • still not my case. Like i said, i have multiple " and ' at same time. Try to make your var like this: ````var = 'TT "TT'T '; you will see what is my problem – Alexis Garcia Mar 24 '20 at 16:16
  • tried both, wont work. First still doenst escape and second gives an php error. What you are doing there i did that in my javascript. ````var htmlText = ''; ```` – Alexis Garcia Mar 24 '20 at 16:30
  • mysql i guess ill do this in another way. ill get it by ajax directly in my JS and forget this data-. Guess it will be better for performance and everything. – Alexis Garcia Mar 24 '20 at 16:38
  • @AlexisGarcia, as you wish, also you can check [this](https://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag) – Aksen P Mar 24 '20 at 16:39