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
https://forums.phpfreaks.com/topic/191040-variable-wont-work-unless-typed/
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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.