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);
// ^^^
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 2 years ago.
Improve this question
hi i want to create string in laravel with foreach and that string must be like this sample:
'item1,item2,item3'
Pay attention to commas and quotation marks.
anyone can help me?
you can use implode function.
Have a look at https://www.php.net/implode
It does exactly that with arrays.
example:
$string = implode(",", $array);
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
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
I would like to find the last hyperlink in a string. The hyperlink may begin with one of the following:
http://
https://
market://
Is there a regex method in PHP to find this?
Thank you.
You can use this negative lookahead based regex:
~\b(?:https?|market)://\S+?(?!.*?\b(?:https?|market)://)~i
$regex = "/(http|https|market)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "http://link.com some text in between https://link.com more text in between market://link.com";
if(preg_match_all($regex, $text, $url))
{
echo $url[0][count($url[0])-1]; // Outputs: market://link.com
}
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
Can I escape any character in string without using symbol "\"?
For example, programming language C# allows specify string, which does not have special characters:
#"c:\webserver\".Equals("c:\\webserver\\") // <== return true
Thanks for answers and sorry for my poor English!
to escape use addslashes(); and stripslashes() to remove added slashes
Do you want to automatically add slashes?
Then you can use built in php function:
addslashes();
http://php.net/manual/en/function.addslashes.php