Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I was reading about how to make preg_replace() act like eval() function if we put the modifier /e this is my code:
$fa= '/site'.$_GET['1st'];
$sh= $_GET['replace'];
$ka= 'admin the best over the rest';
echo preg_replace($fa,$sh,$ka);
if the code running on site, it looks like :
www.site.com/a.php?1st=//e&replace=phpinfo();
but there is a problem that the modifier /e mustn't followed by any thing so it will work if we put || like this :
www.site.com/a.php?1st=||//e&replace=phpinfo();
so here is my question what is || here and how it works ??
im using windows 10 and php version 5.2
| separates alternatives in the regexp; e.g. /abc|def|ghi/ matches either abc, def, or ghi.
When you write 1st=||//e the resulting regexp will be /site||//e. Two of the alternatives are empty strings, which will match the empty strings before and after each character. So this will call phpinfo() for each character in $ka.
Actually, you should get an error because you have two / at the end of the regexp. It should be 1st=/e or 1st=||/e.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to remove this string
page=x
x being a dynamic string (should be a number but can be anything)
from another string, for example
url/?filter=value&page=2&filter2=value
but it can also be:
url/?page=2
So, these are the cases as examples
1. url/?page=2&filter=value
2. url/?page=2
3. url/?filter=value&page=2
4. url/?filter=value&page=2&filter2=value
And they should return:
1. url/?filter=value
2. url/
3. url/?filter=value
4. url/?filter=value&filter2=value
How can I do that with regex?
You've said:
[?&]page=[^&]+
works for you. This will look for an ? or & then page= and anything after it until an &. A bit longer answer though is:
echo rtrim(preg_replace('/([?&])page=[^&]*&?/', '$1', $string), '&?')
which will correctly handle the intro and ending parameter cases.
Demo: https://3v4l.org/SupbH
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I changed the Permalink on WP to get any strings after the path. I use the regex: "yourdomain.com/%postname%-(.*)/"
When I am checking: "yourdomain.com/%postname%-f46eb54b99ce3a9835ea7d63e075d434", it matches.
But when I check:
"yourdomain.com/%postname%-446eb54b99ce3a9835ea7d63e075d434" then it returns "yourdomain.com/%postname%-(.*)/446/".
I think (. *) Will fit in everything, regardless of letters or numbers. I appreciate anyone who can explain it to me.
You should escape all / and ., if you mean them as normal symbols. So, you'll have:
yourdomain\.com\/%postname%-(.*)\/
It must not match
yourdomain.com/%postname%-f46eb54b99ce3a9835ea7d63e075d434
or
yourdomain.com/%postname%-446eb54b99ce3a9835ea7d63e075d434
, because you regex demands / at the end. If it is not obligatory, put ? after the ending \/.
yourdomain\.com\/%postname%-(.*)\/?
tests
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My PHP code needs to decide whether or not a specific IP address is in the string $ipaddress:
preg_match("[1][4]\.[1][0][8]\.[2][4][1]\.[6][9]", $ipaddress )
This doesn't work for some reason. Any ideas why it doesn't work?
[1] and 1 is the same thing, there is no point in creating groups of one element. You can look straight for 14\.108\.241\.69.
What is missing for this to work are the delimiters. When using Perl regular expressions the pattern must be enclosed by delimiters, typically /, # or ~.
preg_match('/14\.108\.241\.69/', $ipaddress)
preg_match('~14\.108\.241\.69~', $ipaddress)
preg_match('#14\.108\.241\.69#', $ipaddress)
Are all valid uses of preg_match().
Because you are looking for a simple piece of string, you don't really need preg_match(), a simple strstr($ipaddress, '14.108.241.69') !== FALSE will do the job just as well and faster.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am practicing for regular expressions.
I was trying to format numbers using PHP and regex. I want to add comma after each 3 digits like this 111222333444 to this format 111,222,333,444 or 11222333444 to 11,222,333,444 by using PHP and Regular expression.
I searched a lot but I could not find exact solution for this.
I know that there is function in php (number_format) to do this but I want to use Regular expression and PHP to do this because I am learning regex and practicing so I want to use regex and php only.
Here is a regex based solution:
$repl = preg_replace('/(?!^)(?=(?:\d{3})+$)/m', ',', $input);
RegEx Demo
Explanation:
(?!^) - Negative lookahead to make sure we are not at start of input
(?=(?:\d{3})+$) - Positive lookahead to make sure there 1 or more of 3 digit sets following current position
Replacement is just a literal comma
More explanation is available at linked demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using the following regex pattern to get values from [URL=http://www.google.com]Google[/URL]
$pattern = '/\[\s*URL(.+?)?\s*\](.+?)\[\s*\/URL\s*\]/i';
It works fine but only if there is only one [URL]...[/URL]. if there is a string having multiple [URL]....[/URL] it gives the wrong values
For example for the following string
$string = '[URL=http://www.google.com]Google[/URL] another website [URL]www.youtube.com[/URL]';
It outputs the first match as ]http://www.freshupnow.com[/URL and leaves the rest URL tags.
I have tried a lot to find the problem in my regex pattern but failed. any one has idea?
Your URL search is working as expected, that is if the /URL you have listed is incorrect, or you are matching to a different pattern than you gave us.
$pattern = '/\[\s*URL(.+?)?\s*\](.+?)\[\s*\/URL\s*\]/i';
would get
([URL=http://www.google.com]Google[/URL] another website [URL)
(]www.freshupnow.com)
([/URL])
for the RegEX above.
If you wanted it to not jump out of the system you would change your RegEx just slightly to
$pattern = '/\[\s*URL([^\]]+?)?\s*\](.+?)\[\s*\/URL\s*\]/i';
This will keep it from jumping out of the []'s when it does a pattern expression.