Regex (.*) and random string [closed] - php

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

Related

how to match /foo/:id/bar?any_query_string_I_want with regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is a page URL and the :id param changes based on the resource and the query string can be all over the map.
How can I match /foo/:id/bar?anything
thanks in advance
It would help to specify any restrictions on :id as you indicated it could change.
Taken at the most direct meaning of the question, the regex
\/foo\/:id\/bar\?.*
will match the exact string /foo/:id/bar? and then anything after.
Explanation:
String matching for the first part, escaping special characters (forward slashes and question mark) with a backslash. A period . indicates any character and an asterisk * indicates any amount of the previous character so .* means match any amount of any character.

Remove dynamic string from another string PHP (regex) [closed]

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

REGEX for alternate characters in PHP [closed]

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
I have a string mystring. I have to replace the alternate characters with x so that final output is mxsxrxnx. All the alternate characters starting from 2nd position are replaced with x. I can do it with loop but is there a Regular expression for that or a better way in PHP? Please help.
Using a reset match meta-character \K you are able to do it:
.\K.
Live demo
PHP:
echo preg_replace('/.\K./', 'x', 'mystring'); // mxsxrxnx

Preg Match / character [closed]

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 have link .
http://www.example.com/about
$_SERVER['REQUEST_URI'] is /about.
So when I do
preg_match("/about/i",$_SERVER['REQUEST_URI']),
it matches the about link.
But if I have to match www.example.com, then I saw that $_SERVER['REQUEST_URI'] returns /.
So I used this code
preg_match("/\//i",$_SERVER['REQUEST_URI']),
but it dosent work. Why?What is the correct solution to preg match /?
You are looking in the wrong variable. $SERVER['REQUEST_URI'] only gives you the relative name of the request, so in your example you will only get "/about". To get the domain name, use the superglobal $SERVER['SERVER_NAME'];.

Regex improvement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have the following regex:
/%%[\s\S]*?%%/i
It's meant to catch these types of strings:
%%test%%
%%test2%%
However, because this regex runs on big strings, sometimes there are mistakes, for example if i have this:
%test%% adhja kshdjah skdja %%test1%% it will return %% adhja kshdjah skdja %%
The strings that it is supposed to catch never have any spaces in them, how can I alter my regex to take only the ones with no spaces?
Use [^%\s] for non space and non %.
/%%[^%\s]*?%%/

Categories