php-general Digest 13 Mar 2012 14:35:55 -0000 Issue 7726
php-general Digest 13 Mar 2012 14:35:55 - Issue 7726 Topics (messages 317001 through 317003): Getting knotted with quotes encoding 317001 by: Arno Kuhl 317002 by: Ashley Sheridan Re: Getting knotted with quotes encoding - (one possible solution) 317003 by: Arno Kuhl Administrivia: To subscribe to the digest, e-mail: php-general-digest-subscr...@lists.php.net To unsubscribe from the digest, e-mail: php-general-digest-unsubscr...@lists.php.net To post to the list, e-mail: php-gene...@lists.php.net -- ---BeginMessage--- I've been battling with quotes encoding when outputting javascript with php. It can't be unique, so I'm hoping someone has a working solution they're willing to share. The following works perfectly as long as there aren't any single quotes in the link text: echo span onclick=\insertLink('$sUrl','$sTitle')\ class='linkSel'$sTitle/span; if $sTitle has the valueWhat's newit outputs: span onclick=insertLink('article/whats-new.html','What#039;s new') class='linkSel'What#039;s new/span It displays fine, but javascript complains with: Expected ')' linkmanager.php Line:525 Char:63 So I fix this by swapping the double and single quotes around: echo span onclick='insertLink(\$sUrl\,\$sTitle\)' class='linkSel'$sTitle/span; Now for that specific link it outputs: span onclick='insertLink(article/whats-new.html,What#039;s new)' class='linkSel'What#039;s new/span And javascript is happy. But elsewhere there's a link Fred Buster Cox and it outputs: span onclick='insertLink(article/fred-buster-cox.html,Fred quot;Busterquot; Cox)' class='linkSel'Fred quot;Busterquot; Cox/span Again it displays fine, but javascript complains with: Expected ')' linkmanager.php Line:743 Char:77 So it looks like I can't have links that include single quotes and double quotes, only one or the other. One work-around I thought of was to convert any link texts that included double quotes into single quotes when the content is posted, and it would then be displayed with single quotes even though the user entered double quotes. It's far from ideal but it would work, though I can think of a few situations where it would be quite confusing to the reader. Are there any other solutions that would allow both types of quotes without any conversions? Cheers Arno ---End Message--- ---BeginMessage--- Arno Kuhl a...@dotcontent.net wrote: I've been battling with quotes encoding when outputting javascript with php. It can't be unique, so I'm hoping someone has a working solution they're willing to share. The following works perfectly as long as there aren't any single quotes in the link text: echo span onclick=\insertLink('$sUrl','$sTitle')\ class='linkSel'$sTitle/span; if $sTitle has the valueWhat's newit outputs: span onclick=insertLink('article/whats-new.html','What#039;s new') class='linkSel'What#039;s new/span It displays fine, but javascript complains with: Expected ')' linkmanager.php Line:525 Char:63 So I fix this by swapping the double and single quotes around: echo span onclick='insertLink(\$sUrl\,\$sTitle\)' class='linkSel'$sTitle/span; Now for that specific link it outputs: span onclick='insertLink(article/whats-new.html,What#039;s new)' class='linkSel'What#039;s new/span And javascript is happy. But elsewhere there's a link Fred Buster Cox and it outputs: span onclick='insertLink(article/fred-buster-cox.html,Fred quot;Busterquot; Cox)' class='linkSel'Fred quot;Busterquot; Cox/span Again it displays fine, but javascript complains with: Expected ')' linkmanager.php Line:743 Char:77 So it looks like I can't have links that include single quotes and double quotes, only one or the other. One work-around I thought of was to convert any link texts that included double quotes into single quotes when the content is posted, and it would then be displayed with single quotes even though the user entered double quotes. It's far from ideal but it would work, though I can think of a few situations where it would be quite confusing to the reader. Are there any other solutions that would allow both types of quotes without any conversions? Cheers Arno -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php You aren't escaping the quotes correctly when they go into your output. You're escaping them for html not javascript. Javascript (like php) escapes single quotes inside a single quote string with a back slash. Thanks, Ash http://ashleysheridan.co.uk ---End Message--- ---BeginMessage--- From: Ashley Sheridan [mailto:a...@ashleysheridan.co.uk] Sent: 13 March 2012 03:25 PM To: a...@dotcontent.net; php-gene...@lists.php.net Subject: Re: [PHP] Getting knotted with quotes encoding Arno Kuhl
php-general Digest 14 Mar 2012 03:01:54 -0000 Issue 7727
php-general Digest 14 Mar 2012 03:01:54 - Issue 7727 Topics (messages 317004 through 317013): Re: questions about $_SERVER 317004 by: Tedd Sperling 317006 by: Tedd Sperling 317007 by: Matijn Woudt 317008 by: Stuart Dallas 317009 by: Tedd Sperling 317010 by: Tim Streater 317013 by: Donovan Brooke Randomly unable to read set variable from class 317005 by: Adrian Basalic 317011 by: Camilo Sperberg set_error_handler() only triggering every Nth time 317012 by: Daevid Vincent Administrivia: To subscribe to the digest, e-mail: php-general-digest-subscr...@lists.php.net To unsubscribe from the digest, e-mail: php-general-digest-unsubscr...@lists.php.net To post to the list, e-mail: php-gene...@lists.php.net -- ---BeginMessage--- On Mar 12, 2012, at 7:12 PM, Tim Streater wrote: ?php function yes ($a) { global $x; if ($a) $x = yes\n; } first (true); echo $x; ? but I haven't looked into $GLOBALS enough to know whether using them instead would have saved my bacon. I'm not sure what would have saved bacon in the above case. I don't see how your example would work. I think it contained a typo. In what I think you were trying to demonstrate, I would just pass $x by reference ($x) -- or -- return $x by value. I would not have used a global, In any event, I seldom use globals anyway. This was more an academic discussion. Cheers, tedd _ tedd.sperl...@gmail.com http://sperling.com---End Message--- ---BeginMessage--- On Mar 12, 2012, at 12:04 PM, Daniel Brown wrote: On Sun, Mar 11, 2012 at 14:16, Tedd Sperling tedd.sperl...@gmail.com wrote: This document clearly states that $GLOBALS is a SuperGlobal -- what am I not understanding here? You are understanding it correctly, the only thing that's missing is the population. The variables are defined (set), but not all are populated. $GLOBALS is a superglobal, you're right; globals set from userland scripts are not superglobals, but do wind up in the $GLOBALS array. Thus, all superglobals are globals, but not all globals are superglobals. So, it's a question of population timing -- I see. Additionally, I like the term userland -- I will use it in class. :-) What would be the opposite term, serverland? Thanks, tedd _ tedd.sperl...@gmail.com http://sperling.com ---End Message--- ---BeginMessage--- On Tue, Mar 13, 2012 at 4:59 PM, Tedd Sperling tedd.sperl...@gmail.com wrote: On Mar 12, 2012, at 7:12 PM, Tim Streater wrote: ?php function yes ($a) { global $x; if ($a) $x = yes\n; } first (true); echo $x; ? but I haven't looked into $GLOBALS enough to know whether using them instead would have saved my bacon. I'm not sure what would have saved bacon in the above case. I don't see how your example would work. I think it contained a typo. In what I think you were trying to demonstrate, I would just pass $x by reference ($x) -- or -- return $x by value. I would not have used a global, In any event, I seldom use globals anyway. This was more an academic discussion. Cheers, tedd I would indeed mark it as bad practice using them. I only use them for debugging purposes. When developing something, you might end up needing some global variable temporary, and you don't want to pass it through a few dozen functions before reaching the one where you need it. - Matijn ---End Message--- ---BeginMessage--- On 13 Mar 2012, at 15:59, Tedd Sperling wrote: In any event, I seldom use globals anyway. This was more an academic discussion. If you're being academic about it please remember that the way PHP defines globals is different to most other languages. PHP: A variable defined at the top-level scope. World: A variable that is visible at every scope. This is an important difference if you ever move from PHP to another language. It ultimately also means that only the superglobals are true globals. The $GLOBALS superglobal contains all variables defined at the top-level scope, including $GLOBALS, so $GLOBALS['GLOBALS']['GLOBALS']['GLOBALS']['_SERVER'] is a perfectly valid, if daft, way of accessing $_SERVER. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ ---End Message--- ---BeginMessage--- On Mar 13, 2012, at 12:20 PM, Stuart Dallas wrote: On 13 Mar 2012, at 15:59, Tedd Sperling wrote: In any event, I seldom use globals anyway. This was more an academic discussion. -snip- It ultimately also means that only the superglobals are true globals. That was my initial statement in this thread. After 47 years of programming, I think I'm beginning to get the idea. :-) As I've said for many years I've learned something new every day of my life -- and I'm getting damned tried of it. Cheers, tedd _