PHP string replace question - php

If I have a string that equals "firstpart".$unknown_var."secondpart", how can I delete everything between "firstpart" and "secondpart" (on a page that does not know the value of $unknown_var)?
Thanks.
Neel

substr_replace
start and length can be computed with strpos. Or you could go the regex route if you're comfortable learning about them.

As long as $unkonwn_var does not contain neither firstpart nor secondpart, you can match against
firstpart(.*)secondpart
and replace it with
firstpartsecondpart

You shoukd use a regexp to do so.
preg_replace('/firspart(.*)secondpart/','firstpartsecondpart',$yourstring);
will replace anything between the first occurence of firstpart and the last of secondpart, if you want to delete multiple time between first and second part you can make the expression ungreedy by replacing (.*) by (.*?) in the expression
preg_replace('/firspart(.*?)secondpart/','firstpartsecondpart',$yourstring);

Related

PHP regex last occurrence of words

My string is: /var/www/domain.com/public_html/foo/bar/folder/another/..
I want to remove the root folder from this string, to get only public folder, because some servers have multiple websites inside.
My actual regex is: /^(.*?)(www|public_html|public|html)/s
My actual result is: /domain.com/public_html/foo/bar/folder/another/..
But i want to remove the last ocorrence, and get somethig like this: /foo/bar/folder/another/..
Thanks!
You have to use a greedy quantifier and to check if the alternative is enclosed between slashes using lookarounds:
/^.*(?<![^\/])(?:www|public(?:_html)?|html)(?![^\/])/
About the lookarounds: I use negative lookarounds with a negated character class to check if there is a slash or the limit of the string at the same time. This way you are sure that for instance html is a folder and not the part of another folder name.
I removed the s modifier that is useless. I removed the capture groups too since the goal is to replace all with an empty string.
The ? makes your expression non-greedy which is not actually what you want here. Try:
^(.*)(www|public_html|public|html)
which should keep going until the last match.
Demo: https://regex101.com/r/v5WbB3/1/

Match first occurrence of string between two strings in text

Problem
I want to select the first contents between two strings. For example:
https://subdomain.domain.com/lobby/het/login?retUrl=https://subdomain.domain.com/lobby/het/responsible?retUrl=https://uat-api.domain.com/forms/authorise-client?retUrl=https://subdomain.domain.com/lobby/het/login?retUrl=https://subdomain.domain.com/lobby/het/testing?retUrl=https://uat-api.domain.com/forms/authorise-client?locale=en-GB&client_id=123&response_type=token&redirect_uri=https://subdomain/rem/rep/sol.html&prompt=0&state=authorise-client
We can see in the above URL that the retUrl occurs many times.
Question
How can I select the contents of ONLY the first retUrl(bold in the above string) using a Regular Expression? So, we need the first string which begins with "retUrl=" and ends on the first occurrence of a ? after it. Is this even possible?
Tries which failed
(?=retUrl=)(.*\n?)(?=\?)
(retUrl=)(.*)$\?
This should work for you:
/retUrl=([^\?]*)/
With ([^\?]*) you can simply say get me everything until a question mark. So you can use the regex with preg_match() which will only give you the first match of that regex.
preg_match only finds the first match. So there you go.
By contrast, preg_match_all finds all matches.

Regex in php: Compulsory second occurence of word

I need to match a few urls for an application I'm working on;
So, I've got this reference string:
content/course/32/lesson/61/content/348
and I need a pattern that matches either
content
OR
content/course/[number]/lesson/[number]/content/[number]
What I've done so far is come up with this pattern:
$my_regex = "/content(\/?|(\/course\/\d{1,4}\/lesson\/\d{1,4}\/content\/\d{1,4}))$/";
which however has the following problem: This string returns a match which should otherwise not:
content/course/32/lesson/61/content
I'm thinking that it's got something to do with the word content repeating twice but I'm not entirely sure.
Any help is much appreciated.
The reason for the match is the alternation.
content\/?$
matches
content/course/32/lesson/61/content
To fix this, add a ^ (beginning of line) to the start of your regex to ensure the entire string is matched and not only the ending:
/^content(\/?|(\/course\/\d{1,4}\/lesson\/\d{1,4}\/content\/\d{1,4}))$/
See it in action
this works:
/(^content\/?|content\/course\/\d{1,4}\/lesson\/\d{1,4}\/content\/\d{1,4})$/

match regex php between two string with string in middle

I would like to get a string made of one word with a delimiter word before and after it
i tried but doen t work
$stringData2 = file_get_contents('testtext3.txt');
$regular2=('/(?<=first del)*MAIN WORD(?=last del)*\s');
preg_match_all($regular2,
$stringData2,
$out, PREG_PATTERN_ORDER);
thank you very much for any help
No quantifier needed, add delimeter at end, put \s inside lookahead.
'/(?<=first del)MAIN WORD(?=last del\s)/'
This regex
(?<=xx)[^\s]*(?=yy)
matches hello in:
xxhelloyy
but fails to match in:
xxhello worldyy
This is probably what you're looking for.
If you want the delimiter string included in the match, then you should not be using lookahead or look or look behind. It should be something rather basic, like this.
/\s?first del MAIN WORD last del\s?/
If you do want to return JUST the MAIN WORD part of the match, then this will work.
/(?<=\s?first del)MAIN WORD(?=last del\s?)/
Put a 'i' at the very end of that to make it case insensitive, if you want. I only mention this, because in the example you gave me above has different case between the example text and the desired response.

How can I check if a string EXACTLY matches a regex pattern?

I'm working on a registration script for my client's product sales website.
I'm currently working on a reference ID input area, and I want to make sure that the reference ID is within the correct parameters of the payment method
The Reference ID will look something like this: XXXXX-XXXXX-XXXXX
I'm trying to use this RegEx pattern to match it: /(\w+){5}-(\w+){5}-(\w+){5}/
This matches it perfectly, but it also matches XXXXX-XXXXX-XXXXXXXXXX
Or at least it finds a match in there. I want it to make sure the entire string matches. I'm not too familiar with RegEx
How can I do this?
You need to use start and finish anchors. Alternatively, if you don't need to capture those groups, you can omit the parenthesis.
Also, the +{5} means match more than once exactly 5 times. I believe you didn't want that so I dropped the +.
/^\w{5}-\w{5}-\w{5}\z/
Also, I used \z so your string doesn't match "abcde-12345-edcba\n".
Use ^ and $ to match the start and end of the input string, respectively.
Also note that your use of + was superfluous, as (\w+){5} means "a word character, at least once, times five" which means it can match at least five times. You probably meant (\w){5} (or just \w{5} if you don't need the backreference; I'll assume in my example that you do).
/^(\w){5}-(\w){5}-(\w){5}$/
put the regular expression in between ^ and $ to match the whole string and check if it matches anything
example:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
Try
/^([\w]{5,5})-([\w]{5,5})-([\w]{5,5})$/i
There are several online regex tester out there, I work with this one before I code.
Enclose it in "^" and "$" thus:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
You need ^ to match the start of the string and $ to match the end:
/^\w{5}-\w{5}-\w{5}$/
Note that (\w+){5} is incorrect because that means five repetitions of \w+, but that in turn means "one or more word characters".
/^(\w){5}-(\w){5}-(\w){5}$/
You need to explicitly say that you want the pattern to start at the beginning of the string and end at it's ending.
You can improve it: /^((\w){5}-){2}(\w){5}$/ ; this way, you can easily modify the number of elements your serial number might have.
Use ^ and $ to mark the start and end of the regex string:
/^\w{5}-\w{5}-\w{5}$/
http://www.regular-expressions.info/anchors.html
In preg, \b marks word boundaries. So you could try with something like
/\b(\w+){5}-(\w+){5}-(\w+){5}\b/

Categories