Regular expression to remove a parameter from url [duplicate] - php

This question already has answers here:
Regular expression to remove one parameter from query string
(9 answers)
Closed 6 years ago.
I have an url which looks like page.php?stuff=stuff&stuff=stuff&order=sometext&page=number
and I want to remove the "order=sometext" part. I'm using this php code :
$link = $_SERVER['REQUEST_URI'];
$re = "/&order=/";
$link = preg_replace($re, "", $link);
And I'm looking for the correct pattern that should be $re to clear the "order=sometext" from the url.
The "&page=number" part has to be kept when present, but isn't always there.
Any ideas ?
Thanks in advance !

The regex should be
(&|\?)order=[^&]+
(&|\?): the order could be first in the list, so we are removing either & or ?.

Actually I made it work using /&order=[^&]*/. :)

Related

Regex to get last word of a url php [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
So i have variable
$var = "how-to-stack-overflow"
I want to ECHO "overflow", or the last word from the string upto a hyphen.
Please show any method to accomplish this, I know this question may have duplicates, but i am a newbie and can't figure out how to put regex expressions using PHP(many valid answers are just the regex without any php), Thanks a lot in advance :)
PLEASE ONLY SHOW USING NEWBIE PROOF PHP
/\w+$/
grab one or more word characters up to the end of the string.
Demo: https://regex101.com/r/onEfF8/1
Edit: Example in php
$var = "how-to-stack-overflow";
$regex = preg_match('/\w+$/', $var, $match);
$match = $match[0];
echo $match;
$var = "how-to-stack-overflow";
$explode=explode('-',$var);
$count=count($explode);
echo $explode[$count - 1];

Regex: Expression selecting more than expected [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I am using the following regex
'/\#(.*)\((.*)\)/'
And I am trying to get #ONE(TWO) one and two from the expression. Which works as long as it's the only time that it can be found before an end of line (I think)
I am quite green with regex and I really cannot understand what I am doing wrong.
What I need is to be able to get all the ONE/TWO couples. Can you please help me.
I am working with PHP and the following function
$parsed_string = preg_replace_callback(
// Placeholder for not previously created article
// Pattern example: #George Ioannidis(person)
'/\#(.*)\((.*)\)/',
function ($matches) {
return $this->parsePlaceholders( $matches );
},
$string
);
The results I am getting from https://regexr.com
* expression is greedy by default. For example such regexp (.*)a will return you bdeabde result on bdeabdea string. You should use special ? symbol for non-greedy * behavior. In your case try to use /\#(.*?)\((.*?)\)/ regexp.

PHP Trouble replacing string containing dash and underscore [duplicate]

This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 6 years ago.
I have tried, read and searched to to avail.
I just need to replace -m_ with -b_ in a string containing an image name / location.
For instance:
I just want to replace;
some-image-name-hyphenated-m_15235101.jpg
with
some-image-name-hyphenated-b_15235101.jpg
Easy right?
$mediumimage = 'some-image-name-hyphenated-b_15235101.jpg';
I tried
$biggerimage = preg_replace("-m_", "-b_", $mediumimage);
and
$biggerimage = preg_replace('-m_', '-b_', $mediumimage);
Also backslashing along with other tries resulting from searches.
Warning: preg_replace(): No ending delimiter '-' found
I don't feel well right now...
I beseech anyone who is smarter than me e.g..... any one here.
Try use $biggerimage = str_replace("-b_", "-m_", $mediumimage);

Remove url and leave only last part of it [duplicate]

This question already has answers here:
Delimiter must not be alphanumeric or backslash and preg_match
(7 answers)
Closed 6 years ago.
I know is something pretty simple for someone who understand well preg_replace but I can't understand how to remove part of url. I have form where user should enter a token. The problem is that some users submitted whole url with token this kind of url
https://example.com/page/token
and
http://example.com/page/token
I want when user submit the form to check if he/she inserted whole url and if is to remove everything and leave only token there
I've tried simple preg_replace like this
$result = preg_replace('https://example.com/page/', '', $str);
But error says
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
Then I've tried to escape those backslashes with \ etc .. but same error occurred.
Any help is appreciated.
Your preg_replace() function should be like this:
$result = preg_replace('$(https|http)://example.com/page/$', '', $str);
You can use $ as a delimiter.

How to extract an id from facebook video link with regular expression? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regex to get value of URL parameter?
If you have a facebook link such as
http://www.facebook.com/photo.php?v=107084586333124
What regular expression would extract the number at the end 107084586333124
edit: I use php
Use PHP's built-in functions to reliably pull query string variables out of a URL. You would use something like:
parse_str(parse_url($url , PHP_URL_QUERY), $v);//where $v is not set yet or an empty array
$v = #$v['v'];//will now contain the value of v or be null if not found
I'm not familiar with the PHP regex engine, but this simple regular expression should do the trick. You will find your number in the first capture group.
v=(\d+)
Replace the following regexes with "".
.*?\?v\=
\D*.*
The remaining data is your number.

Categories