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
Related
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
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
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
First question here!
I have some PHP variables all in the form of the below, in around 7/8 different files.
$this->do['string']
I wish to make them all into the following:
myfunction($this->do['string'])
I understand I have to use regex and the get file contents function, but I'm unsure where to go from here! Would be very grateful if someone can assist!
Thanks in advance
How about:
$result = preg_replace("/(\$this->do\['[^']+'\])/", "myfunction($1)", $inputstring);
Edit according to comment:
In the comment you said that your input string is: $inputstring = "\$this->do['fsdfs']"; (with a backslash before $this). You have then to add the backslash in the regex and also escape it, so the whole instruction becomes:
$result = preg_replace("/(\\\$this->do\['[^']+'\])/", "myfunction($1)", $inputstring);
// ^^^
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 9 years ago.
Improve this question
I have this long number 2241574459 , how can i convert this to string or readable number ?Is there any php functions ?
I know exactly how to accomplish this common task:
$variable = preg_replace(
"/\d{5,}/",
"string or readable number",
$variable
);
This will replace all 5+ digit numbers with string or readable number.
Moral of the story: Make your question clear -- how can anyone know what that integer represents? This is actually the only valid answer to it, as stupid as it is.
$text="2241574459";
$zahl=(int) $text;
Unless I misunderstand your question.
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 9 years ago.
Improve this question
I have this fixed string below.
edbe801bf92fe7b770f72df2d722df0a
And i match what i need with this regex that someone helped me with and i sort of understand.
(?:[^7]*7){4}([^7]*)
Now i looked at character class use today but i dont know how to include a number to match a string like this except with 5 or 3 in same position as the string with 7
How can i also match these numbers and get my text between the last 5 and 3?
edbe801bf92fe5a250e57eb2d522ef0b
edbe801bf92fe3e532b3f0e2f3e0b5fe
All should be captured
2df2d
7eb2d
f0e2f
If you just want to skip the first twenty character and grab the next five, as your sample suggest, you can do it with this simple regular expresion.
^.{20}(.{5})
This regex give the desired output for all your test lines.