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];
Related
This question already has answers here:
regex pattern to match the end of a string
(5 answers)
Closed 3 years ago.
I would like to remove the url which occur last in a string
Input
Hey All the Best. https://google.com/last
Output
Hey All the Best.
Input
Thank you for making me touch 3 Million. https://google.com/first. It’s not MY achievement... it’s your LOVE. https://google.com/second. Gratitude forever https://google.com/last
Output
Thank you for making me touch 3 Million. https://google.com/first. It’s not MY achievement... it’s your LOVE. https://google.com/second. Gratitude forever
You can try the following code.
$str = "Thank you for making me touch 3 Million. https://google.com/first. It’s not MY achievement... it’s your LOVE. https://google.com/second. Gratitude forever https://google.com/last";
//$str = "Hey All the Best. https://google.com/last";
$p = "/(https?:\/\/[^\/]+(?:[\d\w\/\._]*)\s*)$/i";
$result = preg_replace($p, '', $str);
var_dump($result);
The '$' in the regular expression is the last match, and if '$' is not added, all links will be matched out. Corresponding to this is '^', matching the beginning.
Something like this will do:
https?://\S+(?!.*https?://\S+)
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.
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
I have the following string:
$db_string = '/var/www/html/1_wlan_probes.db';
I want to isolate/strip the number character so that I only have the following left:
$db_string = '1';
So far I havn't found an simply solution since the number that needs to be found is random and could be any positive number. I have tried strstr, substr and custom functions but none produce what I am looking after, or I'm simply overlooking somehthing really simple.
Thanks in advance
You should use the preg_match() function:
$db_string = '/var/www/html/1_wlan_probes.db';
preg_match('/html\/(\d+)/', $db_string, $matches);
print_r($matches[1]); // 1
html\/(\d+) - capture all the numbers that come right after the html/
You can test it out Here. It does not matter how long the number is, you're using a regular expression to match all of them.
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=[^&]*/. :)
This question already has answers here:
parse youtube video id using preg_match [duplicate]
(10 answers)
Closed 8 years ago.
I have the following regex which works fine in a regex editor but when I pull it together in PHP i am getting and Unknown modifier '(' error come up.
preg_replace("(\[LINK\])(\S*)(\[\/LINK])", "<a>href=\'$2\'>$2</a>", $xtext);
This is my first question on SO so I hope I have given enough information. From my research I believe I am missing delimiters but tried ~ at the start and the end of the search pattern and still does not seem to work.
try this
preg_replace("/(\[LINK\])(\S*)(\[\/LINK])/", "<a>href=\'$2\'>$2</a>", $xtext);
Note the delimiters
Just try with:
$input = 'foo [LINK]http://google.com[/LINK] bar';
$output = preg_replace('/\[LINK\](.*?)\[\/LINK\]/', '$1', $input);
Output:
string 'foo http://google.com bar' (length=57)