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+)
Related
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];
This question already has answers here:
What does the $1$2$4 mean in this preg_replace?
(3 answers)
Closed 4 years ago.
I want to loop through an array converting specific key/value pairs that contain markup to HTML.
So an example value for $comment['comment_text'] would be:
This has *bolded* text
And should become:
This has <strong>bolded</strong> text
Here's what I've tried:
$pattern = "/\*\b.*?\b\*/i";
$newComment = preg_replace($pattern, "<strong>$&</strong>",
$comment['comment_text']);
And what I get:
This has $& text
I realize I'm mashing up Javascript with PHP, but reading about back references in PHP hasn't made things any clearer.
My strings may have multiple bolded (in markup) instances...
Any help appreciated.
UPDATE:
Apologies - I didn't realize that Stackoverflow was converting asterisks to italics. I converted the example to code.
Also, my confusion came down to the use of $0 vs. $1. Which I still don't fully understand. I thought the numbers referred to the matches in the string...so if you had 5 instances you could refer to them by $0 through $4.
If you use $0 you get:
This has <strong>*bolded*</strong> text
But if you use $1 you get the desired result.
Do this.
$pattern = "/\*\b(.*?)\b\*/";
$newComment = preg_replace($pattern, "<strong>$1</strong>", $comment['comment_text']);
Here $1 refers to the group 1 match. Here I'm supposing that you want to make text between ** bolded.
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 an answer here:
Closed 10 years ago.
Possible Duplicate:
Matching Product Prices from an HTML text
I have a string which is usually, but not always, html page source
I want to extract pricing from within the string. I know this is not an exact science and the combination of currency symbol placement etc is endless but anything better than nothing.
example string:
$string = 'the price is <tag>£10.00</tag>';
So, I am starting with the following regex:
$price = preg_match('#(?:\$|\£|\€|\£|\&\#163;)(\d+(?:\.\d+)?)#', $string);
But of course this only returns the first character.
My question is, is there a way keep going through $string until it finds a certain character? e.g. < or a space? and then return what was found which in this case would be: 10.00
Is this a feasible way of doing this or is there a better way?
Here's the above in an example:
http://ideone.com/u8erb
Read the docs for preg_match, it does not return your match, it only returns if there was a match.
Try this
$string = 'the price is <tag>£10.00</tag>';
$price = preg_match_all('#(?:\$|\£|\€|\£|\&\#163;)(\d+(?:\.\d+)?)#', $string, $matches);
//This will contain your matches
var_dump($matches);
How about using preg_match_all with (\d+(?:\.\d+)?)(?=<\s*/\s*tag\s*>), since the currency may change? Any solution with regex will depend on a set of assumptions, so it's good to get those down first:
Where should you be looking, are these prices occurring within a given div?
What is the full set of possible values?
Try to make your regex as broad as possible, since a common reason it'll fail in the future is because something minor has changed which you haven't considered. If these prices are occurring in a tag with ids and classes, consider using an XHTML parser instead:
http://php.net/manual/en/book.dom.php
http://simplehtmldom.sourceforge.net/