Jump to content

variable wont work unless typed ???


scooby545

Recommended Posts

hi guys,

 

am well confused by this one ... am trying to insert a piece of html code as a variable :

 

$code = "$rbel[mycode]";
$code = str_replace("\n", "", $code);
$code = str_replace("\r", "", $code);
$code = str_replace("'", "'", $code);
$code = str_replace('"', '"', $code);
$code = str_replace('<', '&#60;', $code);
$code = str_replace('>', '&#62;', $code);

echo'
<script type="text/javascript">

var code = "'; echo"$code"; echo'";

</script>
';

 

if i do the variable this way it echo's out in the html like so :

 

var code = "&#60;P&#62;&#60;TABLE style=&#34;BORDER-RIGHT: 0px; PADDING-RIGHT: 5px; BORDER-TOP: 0px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: 0px; WIDTH: 650px; PADDING-TOP: 5px; BORDER-BOTTOM: 0px; BORDER-COLLAPSE: separate&#34; align=center&#62;&#60;TBODY&#62;&#60;TR&#62;&#60;TD style=&#34;BORDER-RIGHT: #aaaaaa 1px dashed; BORDER-TOP: #aaaaaa 1px dashed; BORDER-LEFT: #aaaaaa 1px dashed; BORDER-BOTTOM: #aaaaaa 1px dashed&#34;&#62; Single Column Text Element&#60;/TD&#62;&#60;/TR&#62;&#60;/TBODY&#62;&#60;/TABLE&#62;&#60;/P&#62;";

 

but refuses to work ??

 

if i type the variable in manually like :

 

$code = "$rbel[mycode]";
$code = str_replace("\n", "", $code);
$code = str_replace("\r", "", $code);
$code = str_replace("'", "&#39;", $code);
$code = str_replace('"', '&#34;', $code);
$code = str_replace('<', '&#60;', $code);
$code = str_replace('>', '&#62;', $code);

$code = "&#60;P&#62;&#60;TABLE style=&#34;BORDER-RIGHT: 0px; PADDING-RIGHT: 5px; BORDER-TOP: 0px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: 0px; WIDTH: 650px; PADDING-TOP: 5px; BORDER-BOTTOM: 0px; BORDER-COLLAPSE: separate&#34; align=center&#62;&#60;TBODY&#62;&#60;TR&#62;&#60;TD style=&#34;BORDER-RIGHT: #aaaaaa 1px dashed; BORDER-TOP: #aaaaaa 1px dashed; BORDER-LEFT: #aaaaaa 1px dashed; BORDER-BOTTOM: #aaaaaa 1px dashed&#34;&#62; Single Column Text Element&#60;/TD&#62;&#60;/TR&#62;&#60;/TBODY&#62;&#60;/TABLE&#62;&#60;/P&#62;";

echo'
<script type="text/javascript">

var code = "'; echo"$code"; echo'";

</script>
';

 

it works perfectly, yet echo's out the same in the html ?

 

am sure its somthing to do with the way php is treating the string, but dont know how to fix it  :confused:

 

any help would be greatly appreciated

 

Scooby

Link to comment
Share on other sites

$code = str_replace("\n", "", $code);

$code = str_replace("\r", "", $code);

$code = str_replace("'", "&#39;", $code);

$code = str_replace('"', '&#34;', $code);

$code = str_replace('<', '&#60;', $code);

$code = str_replace('>', '&#62;', $code);

 

$code =  htmlentities($rbel['mycode'], ENT_QUOTES);

 

Can you not just do that?

Link to comment
Share on other sites

yeah all shoes as one line if i echo it out or type it in manually, which is what i cant understand.

If i echo the non working variable out and copy it from the html source code into the php to specify it manually it works like a charm ?

seems to be somthing to do with pulling the value from a mysql database and formatting it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.