PHP loses left square bracket in decoded url - php

I have a url like this:
https://example.com/path/to/folder?param[key][=]=value&foo=bar
In the param definition the second key is an operator, which could be gt, lt, =, etc. The problem appears only when the operator is =.
In Laravel/Lumen I cannot parse the query part of the url because somewhere lost the left square bracket. I have tried urlencode() instead of $request->query() (which gives worst), but the result is the same.
Do you have any idea to get and parse correctly the square brackets definition?

Finally I found a suitable solution. Laravel's $request->getRequestUri() method gives the full url in the right unencoded format.

Related

spacing between square brackets of an array

I have an array where I would like to put spaces between the [] like :
$array[South Africa]=array();
But I can't... why this is not possible?
The correct way of doing this is:
$array['South Africa'] = array();
By not placing quotes around strings, PHP will first check if it is a constant, and if not assume you want to specify the string stated (and generate a warning).
This would work without a space (other than the warning and being bad practise) but with the space PHP thinks the string/constant has ended after 'South' and expects an ]. What you have specified will result in a syntax error:
unexpected T_STRING, expecting ']'
I personally would avoid using spaces for names/keys anyway but the above explains the problem you are having if you must do this.

Escape only particular special chars

I'm using QueryParser::parse() method to get a query from a string search term for my ZendSearch Lucene index. But I've a problem with the following query:
+php +5.7.1)
This throws the QueryParserException with message:
Syntax Error: mismatched parentheses, every opening must have closing.
So I used QueryParser::escape() to escape the string search term before I pass it to QueryParser::parse() but then it escapes everything so this leads to this string:
\\+\\p\\h\\p\\ \\+\\5\\.\\7\\.\\1\\)
Now the QueryParserException has gone but also the possbility of using special chars like +, -, etc.
I look for a way to just escape special chars which will lead to a QueryParserException so in my case the ) should be escaped because there is no opening bracket ) in the query but my two + should stay untouched.
Is there any possbility to achieve this? Building the query itself without parsing is not an option because the search terms are user inputs.
I tried to use QueryParser::suppressQueryParsingExceptions() which probably would be the thing I'm looking for but it has no effect. The QueryParser still throws a QueryParserException although the default value for this is true.
You could use addcslashes
$escapedParenthesis = addcslashes('+php +5.7.1)','\\)');

Optional regex pattern produces no value

I am having a bit of a problem with some regex I did for a project of mine (please keep in mind that I am a beginner at regex which shows in the follwoing example). I am having a bit of a problem with a piece of xml code from which I am trying to extract certain parts of it using an associated pattern.
<banner piclink="pic" urlactive="url_active" urltarget="globaltgt" urllink="globallink" timevar="globaldelay" swf="0" smooth="1" name="name" alt="alternate" />
I am using the following regular expression to obtain the piclink, urlactive, urltarget, urllink and timevar using preg_match_all:
/piclink=\"(?<pic>.+)\".+urltarget=\"(?<target>.+)\".+urllink=\"(?<url>.*)\".+timevar=\"(?<delay>.*)\"/iU
So far so good everything works right however, I am now trying to capture with association the name and alt tags which are optional as in they don't always appear. I have tried to put them in parenthesis followed by a ? to indicate that they are optional like such:
(name=\"(?<name>.*)\")?
However the $matches['name'] array is always empty, I do not know where I am messing up but I have tried all sorts of combinations and all of them result in an empty result except for when I put (?: at the end and encapsulate everything from swf= onwards then it does return like 115 results in the array which is not acceptabe as the result is like $matches['name'][X] = result, where x is sometimes 1 other times its at 109 for some reason.
I agree that something like SimpleXML would be better but if you want to get dirty, you can use lookaheads to try to match with the remaining characters.
/piclink=\"(?<pic>.+)\".+urltarget=\"(?<target>.+)\".+urllink=\"(?<url>.*)\".+timevar=\"(?<delay>[^"]*)\"(?=(.*name=\"(?<name>[^"]*)\")?)(?=(.*alt=\"(?<alt>[^"]*)\")?).*/iU

replicate preg replace with javascript

Is it possible to replicate this with javascript?
preg_replace('/(.gif|.jpg|.png)/', '_thumb$1', $f['logo']);
EDIT - I am not getting this following error for this peice of code,
unterminated string literal
$('#feed').prepend('<div class="feed-item"><img src="'+html.logo.replace(/(.gif|.jpg|.png)/g, "_thumb$1")+'"/>
<div class="content">'+html.content+'</div></div>').fadeIn('slow');
There are a couple of problems with the code you are trying to replicate:
It matches "extensions" even if they aren't at the end of the filename.
The dot in a regular expression matches (nearly*) any character, not just a period.
Try this instead:
'abc.jpg'.replace(/\.(jpg|gif|png)$/, '_thumbs$&')
I'm assuming that the string you are trying to replace contains only a single filename.
*See the documentation for PCRE_DOTALL.
Yes, except that in JavaScript, replace is a string's method, so it would be rearranged a little (also, the array/object notation is slightly different):
f.logo.replace(/\.(gif|jpg|png)/, '_thumb.$1');
more info
somestringvar.replace(/(.gif|.jpg|.png)/, replacementValue)

What does {0} mean in a PHP statement?

Apols if this has been asked before. I am using someone else's PHP code and came across the following line:
if($_GET['file']{0}=='.') die('Wrong file!');
The if, $_GET and die I understand, but what is the meaning of the {0} after the $_GET['file']? I've looked through a number of tutorials and didn't come across the answer.
TIA.
$str{0} will return the first character/byte of a string. But the syntax $str{0} is deprecated in favor of $str[0]:
Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
If you’re working with multi-byte characters, use mb_substr instead.
The {0} is the same as [0]. So, $_GET['file']{0} is getting the zeroth character from $_GET['files'].
It's shorthand for accessing the first character of the string. $_GET['file']{1} would be the second character, and so on. So in your example it's checking to see whether the first character is a dot, and if so, exiting; presumably to avoid people passing paths in the URL such as ../../../etc/passwd.
As others have said, it's looking at string position 0 in the variable $_GET['file'] and throwing an error if that happens to be a dot.
This looks like a (relatively crude) way of preventing hack attacks by blocking the user if he tries to access a file that starts with a dot.

Categories