EzDevInfo.com

brackets

An open source code editor for the web, written in JavaScript, HTML and CSS.

PHP conditionals, brackets needed?

I was just browsing a forum and someone asked about a PHP file they had found on the web. It has several spots like this in the code:

if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);

I have always thought brackets are needed to enclose what you want to do if the condition is true. Is there some other alternative, such as if it is on the same line you don't?

There is also another line like this: if ($action != ""): mail("$adminaddress","Visitor Comment from YOUR SITE",

My instinct is to say this wouldn't work, but I also don't know if it is an outdated PHP file and it used to work?


Source: (StackOverflow)

How to prevent Sublime Text 2 from swallowing closing brackets, quotes and parentheses?

Sublime has this behaviour which is really annoying sometimes when you have to type in constructions with lots of brackets. When you type ( it adds () and puts the cursor in the middle, all fine, if you however will type ) it will silently swallow the closing bracket.

This is really annoying when typing long regexps because the brackets gets unbalanced pretty quick and this is driving me crazy. So you end up with constructions like (([a-z]).

So the question is - is there a way to disable this? If I type a closing bracket I want it to stay, not be swallowed.

I have checked through Sublime configs, googled, but nobody seems to mind this behaviour. Am I using it wrong?

Update

You might want to check out Sublime: Jump out of matching brackets shortcut as well.

Full version that allows you to type through with () but will not swallow the closing symbol if you have entered any text:

  { "keys": ["\""], "command": "insert", "args": {"characters": "\""}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "[^\"]$", "match_all": true }
      ]
  },
  { "keys": [")"], "command": "insert", "args": {"characters": ")"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "[^(]$", "match_all": true }
      ]
  },
  { "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true }
      ]
  },
  { "keys": ["'"], "command": "insert", "args": {"characters": "'"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "'$", "match_all": true }
      ]
  },
  { "keys": ["]"],"command": "insert", "args": {"characters": "]"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "[$", "match_all": true }
      ]
  },
  { "keys": ["}"], "command": "insert", "args": {"characters": "}"}, "context":
      [
          { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
          { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
          { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true },
          { "key": "preceding_text", "operator": "regex_contains", "operand": "{$", "match_all": true }

      ]
  }

Source: (StackOverflow)

Advertisements

Regex pattern to return text from within parenthesis

I am looking for a regex pattern that will return me the contents of the first set of parenthesis in a string.

For example,

text text text text (hello) text (hello2) (hello3) text

will return "hello"

Does anyone know what the pattern looks like for c#?


Source: (StackOverflow)

Python parsing bracketed blocks

What would be the best way in Python to parse out chunks of text contained in matching brackets?

"{ { a } { b } { { { c } } } }"

should initially return:

[ "{ a } { b } { { { c } } }" ]

putting that as an input should return:

[ "a", "b", "{ { c } }" ]

which should return:

[ "{ c }" ]

[ "c" ]

[]

Source: (StackOverflow)

Is it ok if I omit curly braces in Java?

I've searched for this, but couldn't find an answer and for whatever reason I was too ashamed to ask professor, due to that feeling when hundreds of people stare at you...

Anyhow, my question is what's the importance of having brackets? Is it OK if I omit them? Example:

for (int i = 0; i < size; i++)  {
   a += b;
}

vs

for (int i = 0; i < size; i++)
   a += b;

I know both of them will work, but if I omit the brackets (which I tend to do a lot, due to visibility) will that change anything, anything at all? As I said, I know it works, I tested it dozen of times, but now some of my uni assignments are getting larger, and for some reason I have irrational fear that in the long run, this my cause some problems? Is there a reason to fear that?


Source: (StackOverflow)

Tournament bracket placement algorithm

Given a list of opponent seeds (for example seeds 1 to 16), I'm trying to write an algorithm that will result in the top seed playing the lowest seed in that round, the 2nd seed playing the 2nd-lowest seed, etc.

Grouping 1 and 16, 2 and 15, etc. into "matches" is fairly easy, but I also need to make sure that the higher seed will play the lower seed in subsequent rounds.

An example bracket with the correct placement:

1 vs 16
            1 vs 8
8 vs 9
                        1 vs 4
4 vs 13
            4 vs 5
5 vs 12
                                    1 vs 2
2 vs 15
            2 vs 7
7 vs 10
                        2 vs 3
3 vs 14
            3 vs 6
6 vs 11

As you can see, seed 1 and 2 only meet up in the final.


Source: (StackOverflow)

Is there a way for NetBeans to automatically create brackets in a seperate line? [details inside]

When I create a new class for instance, I get this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;

/**
 *
 * @author Sergio
 */
public class WordManipulations{        
}

I hate it when brackets are placed this way. Is there a way to make it create things like this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;

/**
 *
 * @author Sergio
 */
public class WordManipulations 
{

}

Source: (StackOverflow)

Codeigniter - brackets with activerecord?

How to have round brackets symbol in Codeigniter's active record SQL queries? e.g. how to accomplish

SELECT * FROM `shops` WHERE (`shopid` = '10' OR `shopid` = '11') AND `shopid` <> '18'

Source: (StackOverflow)

operator[]= overload?

Okay, I'm trying to make a quick little class to work as a sort of hash table. If I can get it to work then I should be able to do this:

  StringHash* hash = new StringHash;
  hash["test"] = "This is a test";
  printf(hash["test"]);

And it should print out "This is a test".

It looks like I have 2 problems at this point. Firstly I did this:

const char* operator[](const char* key) {
  for(int i = 0; i < hashSize; ++i) {
    if(strcmp(hkeys[i], key) == 0) {return values[i];}
  }
  return NULL;
}

But when I try to look up a value the compiler complains that

error: invalid types `StringHash*[const char[5]]' for array subscript

Secondly operator[]= does not appear to be the correct syntax here. The only other thing I could find was &operator[] but I don't think that will work since I have to code the lookup procedure??? (Isn't that syntax just used to return an array item reference?)

Is what I'm trying to do here even possible? Any advice appreciated. :)


Seems to be some confusion about what I'm trying to do. I'll post my code:

http://pastebin.com/5Na1Xvaz


Finished product after all the help:

http://pastebin.com/gx4gnYy8


Source: (StackOverflow)

Why does this semicolon appear if I use jQuery serialize() on inputs with square brackets in the name?

I have the following HTML:

<input type="checkbox" id="options_1" value="options_1" name="options[]">  
<input type="checkbox" id="options_2" value="options_2" name="options[]">  
<input type="checkbox" id="options_3" value="options_3" name="options[]">  

I check the first two options and send it to the server via ajax in jQuery:

$.ajax({
    type: "POST",
    url: "myfile.php",
    data: {
        'options':$('input[name="options[]"]').serialize()
    },
    dataType: 'json',
    beforeSend: function(){
           //do some stuff
    },
    success: function(msg){
        //do some stuff
    }
});

Firebug shows me the data that has been posted:

options options%5B%5D=options_1&options%5B%5D=options_2
So far, so good.

In myfile.php I get the POST-Variable like this:

$options = $_POST['options'];

Now when I echo $options I get this:

"options[]=options_1&options;[]=options_2"

Where does this semicolon in front of the second pair of brackets come from? This is driving me crazy.

I already used utf8_decode on the POST data as well as urldecode and rawurldecode. Nothing changes. I also escaped the square brackets in the ajax call like this:

data: {
    'options':$('input[name="options\\[\\]"]').serialize()
},

That didn't help either. Any ideas anyone?


Source: (StackOverflow)

White spaces before and post equal sign, brackets and other

I use Visual studio 2012 and plugin Visual assist X ver. 1916. I have two questions.

1) When I write equal sign (=), I want automatic insertion of white spaces before and after the sign (I must always insert by space on keyboard...). Is it possible?

Example:

int variable=167;
->
int variable = 167;

or

"=" -> " = "

2) I want automatic insertion of white space before and after brackets and before and after commas. Is it possible?

Example:

void fun(int param1,int param2);
->
void fun ( int param1, int param2 );

Source: (StackOverflow)

Autocomplete Method Brackets

Using: Visual Studio Pro 2013

Previous research: [1], [2], [3]

I'm used to working in Java with Eclipse.

My usual flow is: object. CTRL+SPACE + ENTER which autocompletes the method and places the correct curly brackets & method inputs in there:

object.myMethod();
or
object.myMethod(input1,input2);

I'm trying to get the same behaviour with VS in C#. I can get the method, but it doesn't want to include the final brackets for some reason.

I get:

object.myMethod

Is there a way to enable this?


Source: (StackOverflow)

Python: What does the use of [] mean here?

What is the difference in these two statements in python?

var = foo.bar

and

var = [foo.bar]

I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case?

For example: if foo.bar = [1, 2] would I get this?

var = foo.bar #[1, 2]

and

var = [foo.bar] #[[1,2]] where [1,2] is the first element in a multidimensional list

Source: (StackOverflow)

regular expressions with escaping round brackets

I am trying to write the expresion that will accep fallowing phone numbers format:

508 736 756
505050505
+48 505 505 505
(+48) 505 505 505
++48 505 505 505
(++48) 505 505 505
(23)692 36 99
23 692 36 99

I have wrote the fallowing expresion

^(([+]{0,2}?)?([+]{0,2}?)?([0-9 ]+)?)$

How ever this expresion covers only the format listed below:

508 736 756
505050505
+48 505 505 505
++48 505 505 505
23 692 36 99

I have issue with escaping the round brackets characters ( , ) that is why i cant cover the fallowing formats

(+48) 505 505 505
(++48) 505 505 505
(23)692 36 99

I have tried escaping them with backslash but it wouldn't work for some reason.

...[(]?[+]{0,2}[)]?...

DEMO: https://regex101.com/r/hY5tG4/2


Source: (StackOverflow)

Special uses of this syntax in PHP? (Triple 'Angle Brackets')

Given the following code:

$myString = <<<script
   .
   .
   .
 script;

Thanks to the answers on the original version of this question, I understand <<< to be heredoc syntax, treated as double quotes without the need for escaping quotes.

Taking this a step further, how is this best exploited? Specifically, should this ease the strain of dealing with mixed quote strings containing code syntax?

i,e..

attribute="name-like string" attribute="property: 'value("value")';"

The thought is this may be useful (if implemented the way I am now guessing) especially when dealing with greater complexity and/or looking out for code injection. Again, looking for any scenarios where the heredoc for is particularly useful or exploitable.


Source: (StackOverflow)