Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-30-2010, 04:29 PM   PM User | #1
steadythecourse
New Coder

 
Join Date: Mar 2010
Location: USA, NJ
Posts: 42
Thanks: 16
Thanked 0 Times in 0 Posts
steadythecourse is an unknown quantity at this point
conditional with no curly brackets

I came across this function, and I don't understand how you can have a conditional statement without any curly brackets. Can someone give me and explanation as to why and how this works.

PHP Code:
// Strip Input Function, prevents HTML in unwanted places
function stripinput($text) {
    if (
ini_get('magic_quotes_gpc'))
    
$text stripslashes($text);
    
$search = array("\"""'""\\"'\"'"\'""<"">""&nbsp;");
    
$replace = array("&quot;""'""\", "&quot;", "", "&lt;", "&gt;", " ");
    $text = str_replace($search, $replace, $text);    
    return $text;

If I include the curly brackets where I instinctively think they should go as shown below it does not replace the $search values with the $replace values.

PHP Code:
// Strip Input Function, prevents HTML in unwanted places
function stripinput($text) {
    if (
ini_get('magic_quotes_gpc'))   {
    
$text stripslashes($text);
    
$search = array("\"""'""\\"'\"'"\'""<"">""&nbsp;");
    
$replace = array("&quot;""'""\", "&quot;", "", "&lt;", "&gt;", " ");
    $text = str_replace($search, $replace, $text);    
       }
    return $text;

Thank you
steadythecourse is offline   Reply With Quote
Old 07-30-2010, 04:34 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Conditions without curly braces only apply to the next line in the code. I personally do not omit the curly braces regardless of if its one line due to the readability aspect.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 07-30-2010, 04:41 PM   PM User | #3
steadythecourse
New Coder

 
Join Date: Mar 2010
Location: USA, NJ
Posts: 42
Thanks: 16
Thanked 0 Times in 0 Posts
steadythecourse is an unknown quantity at this point
Hi Fou-Lu,

So the following would be the correct representation using brackets?


PHP Code:
// Strip Input Function, prevents HTML in unwanted places
function stripinput($text) {
    if (
ini_get('magic_quotes_gpc'))   {
    
$text stripslashes($text);
        }
    
$search = array("\"""'""\\"'\"'"\'""<"">""&nbsp;");
    
$replace = array("&quot;""'""\", "&quot;", "", "&lt;", "&gt;", " ");
    $text = str_replace($search, $replace, $text);    
    return $text;

steadythecourse is offline   Reply With Quote
Old 07-30-2010, 04:45 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Thats correct. Its also writable as ternary:
PHP Code:
// or can call get_magic_quotes_gpc() which returns a boolean
$text ini_get('magic_quotes_gpc') ? stripslashes($text) : $text

The search and replace seems a little overboard though. You can run that through htmlspecialchars or htmlentities instead of doing the work yourself. It looks like a syntax error on the forums here, but I'm betting you that its no problem at all.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
steadythecourse (07-30-2010)
Old 07-30-2010, 04:46 PM   PM User | #5
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,466
Thanks: 90
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
the placement of the curly brackets may be correct but the reason the function is not working is because you have unclosed quotes

PHP Code:
"\"   // string is not closed because of the escape char \" 
PHP Code:
"\\" // this string is closed. 
__________________
You can not say you know how to do something, until you can teach it to someone else.
timgolding is offline   Reply With Quote
Old 07-30-2010, 04:50 PM   PM User | #6
steadythecourse
New Coder

 
Join Date: Mar 2010
Location: USA, NJ
Posts: 42
Thanks: 16
Thanked 0 Times in 0 Posts
steadythecourse is an unknown quantity at this point
Thanks Fou-Lu,

It works fine, and I like the ternary version.

steadythecourse
steadythecourse is offline   Reply With Quote
Old 07-30-2010, 04:56 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by timgolding View Post
the placement of the curly brackets may be correct but the reason the function is not working is because you have unclosed quotes

PHP Code:
"\"   // string is not closed because of the escape char \" 
PHP Code:
"\\" // this string is closed. 
I noticed that too, but I'm thinking that its a strip from the forum. I've seen a very similar problem to this not that long ago which causes the pattern typed replacements to show incorrectly when parsed by the [php] tags.
Although for the OP, if it actually is like that (just the single), then yes its definitely a syntax error and needs to be corrected. They can of course just be removed since it searches and replaces the same char.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 07-30-2010, 05:13 PM   PM User | #8
steadythecourse
New Coder

 
Join Date: Mar 2010
Location: USA, NJ
Posts: 42
Thanks: 16
Thanked 0 Times in 0 Posts
steadythecourse is an unknown quantity at this point
Yes,

the forum is causing the problem. I can't get it to display correctly.

thanks again
steadythecourse is offline   Reply With Quote
Old 07-30-2010, 05:15 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by steadythecourse View Post
Yes,

the forum is causing the problem. I can't get it to display correctly.

thanks again
I have yet to determine WHAT it is that PHP is doing when it destroys those, but can make it difficult if your trying to debug things like regex when you can't see it properly >.<
Thanks for confirming that.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:03 PM.


Advertisement
Log in to turn off these ads.