Alright, this problem seems to be way above my head!
I have this code:
$request=preg_replace('$(^'.str_replace('$','\$',$webRoot).')$i','',$requestUri);
This throws me an error:
preg_replace(): No ending delimiter '$' found
But here's the thing, that ending delimeter is certainly there.
After that function call I echoed out the following:
echo $webRoot;
echo $requestUri;
echo '$(^'.str_replace('$','\$',$webRoot).')$i';
This is the result of those echoes:
/
/en/example/
$(^/)$i
What is funny is that if I do this directly:
preg_replace('$(^/)$i','',$requestUri);
..it works. But this also fails:
$tmp=str_replace('$','\$',$webRoot);
preg_replace('$(^'.$tmp.')$i','',$requestUri);
And just to be thorough, I also tested what echo $tmp gives, and it does give the proper value:
/
Is it a bug in PHP in Windows? I tried it out on Linux server and it worked as expected, it didn't throw this error. Or am I missing something?
Just to make sure, I even updated PHP to latest Windows version (5.4.2) and the same thing happens.
Well, I personally would use another character as a delimiter like '#' since the $ char is a regexp special char which matches at the end of the string the regex pattern is applied to. That said the few times I had to work on windows servers I found that every regular expressions has to be passed through preg_quote function, nevermind if it contains or not regexp special chars.
$request=preg_replace('#(^'.preg_quote($webRoot).')#i','',$requestUri);
abidibo's answer is correct, but apparently the problem was caused by a bug in str_replace() function. For some reason, in Windows Apache and nginx, this function corrupts the string and pads it with symbols that cannot be read.
Related
I was using the delimiters described in this answer:
Here is shell php I'm using to demonstrate the problem:
I'm using a function on a web project that replaces a namespace syntax for a path, eg:
\org\project\namespace\access
should be converted to:
/org/project/namespace/access
But I tried many ways, some threw the following warning:
Warning: preg_replace(): No ending delimiter '/' found in...
Which I don't want to show.
The warning above shows when using this as regex: /\\/
extra: (please look at the image) why it shows a bad encoded string?
UPDATE: why does it not work as this PHP Live Regex ?
You need to use "/\\\\/" instead of "/\\/" because \\ will produce \ (a single backslash) in a PHP string literal.
See http://php.net/manual/en/language.types.string.php
Why are you using regular expressions for such simple case? Stick to str_replace() or strtr():
echo strtr($str, ['\\' => '/']);
Sorry for this question as it is very specific.
I have a JQuery validation RegEx that I would like to use on the back end too:
var forNames = new RegExp("^[^0-9<>'\"/;`%]*$");
I tried in PHP
preg_match('/^[^0-9<>\'\"/;`%]{2,42}$/', $first_name) // I also want to keep the length between 2 and 42 here)
but it does not work, I get Unknown modifier ';' in
The other question, similar to this one is what this person is asking here
Converting Javascript Regex to PHP
I tried his solution, copying the php email validation regex into JQuery with no luck
Thank you
Ps I just unedited what i had added to the regex cause i didnt see it already had answers and it was confusing
You need to escape the / character in your PHP regex string, because that's also the character which is used to signify the end of a regexp (it's called a delimiter):
preg_match('/^[^0-9<>\'\"/;`%]{2,42}$/', $first_name)
^
becomes:
preg_match('/^[^0-9<>\'\"\/;`%]{2,42}$/', $first_name)
The reason you didn't need to do this in your JavaScript code is that you used the RegExp constructor, which essentially automatically escaped it for you. If you had used a RegExp literal you would have had to escape it too:
var forNames = /^[^0-9<>'\"\/;`%]*$/;
As #DelightedD0D commented, make sure to test your RegExp with an interactive tool like regex101, it supports both PHP and JS style regexp and is actually how I was able to catch your error so fast.
EDIT: I found a solution I didn't expect. See below.
Using regex via PHP's preg_match_all , I want to match a certain url (EDIT: that is already escaped) in a string formatted as json. The search works wonderfully in Notepad++ (using regex-matching, of course) but preg_match_all() just returns an empty array.
Testing on tryphpregex.com I found out that somehow my usual approach to escaping a backslash gives a pattern error, i.e. even the simple pattern https:\\ returns an empty result.
I'm utterly confused and have been trying to debug for too long so I may miss the obvious. Maybe one of you can see the simple error?
The string.
The pattern (that works fine in Notepad++, but not in PHP):
%(https:\\/\\/play.spotify.com\\/track\\/)(.*?)(\")%
You don't need to escape the slash in PHP %(https://play.spotify.com/track/)(.*?)(\")%
The Backslash before doule quote is only needed if you enclosures are double quotes too.
Found a solution to my problem.
According to this site, I need to match every backslash with \\\\. Horrible, but true.
So my pattern becomes:
$pattern = "%(https:\\\\/\\\\/play\.spotify\.com\\\\/track\\\\/)(.*?)(\")%";
Please observe that I tried to find a pattern inside a string that didn't contain clear urls, but urls containing escape characters (it was a json-output from spotify)
Just a note to begin I am aware that ereg_replace() is deprecated, since POSIX is no longer being used. But in "Beginning PHP and MySQL" by W Jason Gilmore, Gilmore emphasizes that although POSIX isn't to be used, an understanding is still necessary as a means of conversion to Perl. So once again I understand it's deprecated but since I'm trying to understand everything in the book I might as well understand this.
So the example is as follows:
<?php
$text = "This is a link to http://www.example.com/.";
echo ereg_replace("http://([a-zA-Z0-9./-]+)$", "\\0",
$text);
?>
//Output
This is a link to http://www.example.com/..
So I understand the majority of code in the above example, my problem lies with the ./- and the output. For the ./- I tried to think according to quantifiers where . = between, so everything between [:alnum:] and / is replaced. I also thought maybe ./- are characters within the range which would also be replaced since [:alnum:] doesn't include punctuation. For verfication I looked at the output but theres no - present. If only the / is replaced than the code would make sense, since /0 outputs http://www.example.com/ but than the problem lies with the missing - which I presume to be pertinent to the brackets rather than as a quantifier.
My other question is in regards to the output, if the function returns the string with the modified string why does the period which was present in the original string appear after the second /0, not the first, if its the original text, why does the tag follow it and not precede it?
Just for some quick background, I have a basic understanding of php,html,css,javascript,C++ and I'm reading this for a more in depth understanding of php and an introduction to MySQL, so unfortunately explanations which are entirely advanced code/concepts go right over my head.
why does the period which was present in the original string appear after the second /0, not the first
This is not the case, because the actual output is:
This is a link to http://www.example.com/.
The period is included in both the attribute as well as the tag contents.
my problem lies with the ./- and the output
When present inside a character set, ./- means to match either a period, forward slash or a dash; it's important to note that the dash must appear at the end of the character set to avoid ambiguity.
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.