Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Photo
- - - - -

Php Long Variables


5 replies to this topic

#1 sparkx

sparkx

    Sparkx

  • [HOSTED]
  • 376 posts
  • Gender:Male
  • Location:Dana Point, CA, USA
  • myCENTs:55.07

Posted 16 August 2007 - 03:28 PM

Don't ask why but I need to make a long variable that contains ' " ; and ) meaning I cannot use:
$var=('Test');
It will create an error. Here is an example of what I am talking about:
$var=("I want to use ' and " while allowing html and not using htmlspecialchars");
Basicly is what I want is a <<<END variable. I know you can do this in an echo like:
echo<<<END
This is some echo. I can use ' " ) and; without using htmlspecialchars
END;
Is there a way to make a simalur code that works with variables? Do you get what I mean? That would really help me out.
Thanks,
Sparkx
Note: I need it for HTML and some HTML strings use both ' and " so I cannot use just one and make the string with the other.

#2 pyost

pyost

    Way Out Of Control - You need a life :)

  • Members
  • 1,090 posts
  • Gender:Male
  • Location:Belgrade, Serbia
  • myCENTs:67.69

Posted 16 August 2007 - 07:45 PM

According to php.net, there are only three ways of storing a string. The first one uses single quotes, the second one double quotes, and the third one uses the heredoc structure (<<<END ... END;). There are slight difference between the first two when it comes to escaping, but either way you would have to use \' or \". I think that heredoc is your best option, as it doesn't require any escaping and you can insert variables without any problems. Yes, it is somewhat bulky, but is great for outputting HTML code.

#3 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 17 August 2007 - 06:34 PM

You can use the Chr() php function, this function returns a one-character string that contains the character specified by the ascii code passed to the function as a parameter.

For example try this code:
$var=chr(40) . chr(39) ."Test" . chr(39) . chr(59) . chr(41);
echo $var;

You can get all the ascii codes with the following function:
for($x=32; $x<256;$x++) {
echo "code = $x : char = " . chr($x) . "<br />";
}
I start the for with the value 32 -it is the space character- because all the prior characters are control characters that you don't need i guess, if you want change it to 1 and see what i'm talking.

BTW, sorry but right now i don't have more time to test if it would work fine with HTML code, maybe later i can.

Best regards,

#4 sparkx

sparkx

    Sparkx

  • [HOSTED]
  • 376 posts
  • Gender:Male
  • Location:Dana Point, CA, USA
  • myCENTs:55.07

Posted 20 August 2007 - 03:14 PM

Thanks pyost and voxPeru. I am going to look at php.net for the <<<END and END; method for a string. If that does not work I many need to use the advanced looking chr() string. I know a little about the <<<END method and how it stops most php enjections ect.
Thanks again,
Sparkx

#5 vujsa

vujsa

    Absolute Newbie

  • [MODERATOR]
  • 888 posts
  • Gender:Male
  • Location:Indianapolis, Indiana, USA (Midwest)
  • myCENTs:35.43

Posted 02 October 2007 - 12:48 AM

I'm not sure if you ever got the answer that you needed. It is possible to simply escape the problematic quote like so:
$var = "That's the \"BEST\" option I have ever seen!";
echo $var;
Outputs:
Thats' the "BEST" option I have ever seen!

--OR--

$var = 'That\'s the "BEST" option I have ever seen!';
echo $var;
Outputs:
Thats' the "BEST" option I have ever seen!

--OR--

You can of course do it the easy way which is to use the heredoc method:
$var = <<<EOD
Thats' the "BEST" option I have ever seen!
EOD;
echo $var;
Outputs:
Thats' the "BEST" option I have ever seen!


For more information, here is a link to the PHP.net syntax for this option:
http://php.net/manua....syntax.heredoc

I hope that this helps you out. ;)

vujsa

#6 vizskywalker

vizskywalker

    Techno-Necromancer

  • Members
  • 1,018 posts
  • Location:The Net

Posted 02 October 2007 - 01:58 AM

Cool. I didn't know you could use heredoc to define variables. I never assumed you couldn't, just never thought about it. Useful to know.

~Viz



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users