Regex to match PHP Function definition [closed] - php

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 6 years ago.
Improve this question
I need a regex string to match PHP Function definitions like:
function anyname(any, number, of, params) {
in editors like Geany or Notepad++ and insert a static text (ofcourse PHP Code) such as var_dump(debug_backtrace()); inside the matched function definitions.
As I have to debug a very large PDF Class file of size around 1.24MB and having more than 60 function definitions.
Can anyone help ?

How about this:
function\s+.*?\)\s*{
I did a rough test and it works on my Notepad++.
EDIT
If you need to replace these with the debug backtrace, do this:
Find:
function\s+(.*?)\)\s*{
Replace:
function \1\) {\nvar_dump\(debug_backtrace\(\)\);
Tested with Notepad++.
Explanation:
The regex itself is nothing too fancy. The .*? simply means "Match anything, ungreedy". It will match everything until it sees a closing bracket and stop.
During the find and replacement, the .*? is enclosed into a bracket, because we need to take this out as the texts we need to preserve, and then fill it up in the \1 position in the replacement.

Related

what does || symbol means in Preg_replace() 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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I was reading about how to make preg_replace() act like eval() function if we put the modifier /e this is my code:
$fa= '/site'.$_GET['1st'];
$sh= $_GET['replace'];
$ka= 'admin the best over the rest';
echo preg_replace($fa,$sh,$ka);
if the code running on site, it looks like :
www.site.com/a.php?1st=//e&replace=phpinfo();
but there is a problem that the modifier /e mustn't followed by any thing so it will work if we put || like this :
www.site.com/a.php?1st=||//e&replace=phpinfo();
so here is my question what is || here and how it works ??
im using windows 10 and php version 5.2
| separates alternatives in the regexp; e.g. /abc|def|ghi/ matches either abc, def, or ghi.
When you write 1st=||//e the resulting regexp will be /site||//e. Two of the alternatives are empty strings, which will match the empty strings before and after each character. So this will call phpinfo() for each character in $ka.
Actually, you should get an error because you have two / at the end of the regexp. It should be 1st=/e or 1st=||/e.

Regex (.*) and random string [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 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

PHP - preg_match explanation [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 5 years ago.
Improve this question
Im a beginner in PHP I just want to ask can someone explain to me this line of code.
(preg_match('/^\w{5,}$/', $username))
Thankyou in advance. :) Your answer is so much appreciated. :)
Your PHP match string is
/^\w{5,}$/
and a PHP match string is surrounded by / characters which are not part of the RegEx string itself.
According to the comments your problem is about understanding regular expressions, not PHP.
^ is the beginning of the line, correct
$ is the end of the line, correct
\w Any word character (letter, number, underscore)
a{5,} does mean 5 or more characters 'a'
Therefore: If there are 5 or more any word characters in the username the function returns a positive result.
Or even easier: A username needs to contain at least five any word characters.
Learn more about regular expressions and how they work. Some explanation can be found in this comment.

PHP Place each word in a paragraph in a <span> without punctuation [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
How do I place each word in a paragraph in its individual span tag? Currently I am using explode() and using a space to do this, but the I get the following:
"<span>Hello,</span> <span>I</span> <span>am</span> <span>Alec.</span>"
I will be using a dictionary lookup for each word, but "Hello," will not find what I'm looking for. Furthermore, I am stylizing the span depending on the word selected (through an onClick), so I don't want the comma to be part of the span.
Eventually, I want the following code to populate:
"<span>Hello</span>, <span>I</span> <span>am</span> <span>Alec</span>."
Theoretically it would be nice to do this without using explode() since I have to add all of these characters back into the text, but outside of the <span>. Is there a function to accomplish this?
Thank you!
Edit: I will be using this for everyday writing in different languages, so hyphenated words or letters outside of "A-Za-z" are expected.
preg_replace() can do that:
$str = 'Hello, I am Alec.';
echo preg_replace('/(\w+)/', '<span>${1}</span>', $str);
Output:
<span>Hello</span>, <span>I</span> <span>am</span> <span>Alec</span>.

Making all occurrences of a specific word capital? [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've got a few recurring words on my website that appear quite frequently. Is there any way to make all occurrences of that specific word all capitals?
You can use str_ireplace. It's the case-insensitive version of str_replace. Once you have the content from the database inside of a variable you can do the following before you output it:
$content = str_ireplace('replace', 'REPLACE', $content);
This is searching for the case-insensitive text replace inside of $content and changing all occurrences to REPLACE. This will not only change whole words, it will change partials as well, for example it will change replaced to REPLACEd.
If you would like to only select whole words you will need to use regular expressions with the preg_replace function. Example:
$content = preg_replace('/(\W|^)replace(\W|$)/i', '${1}REPLACE${2}', $content);
This matches all case-insensitive occurrences of the word replace that are either next to a non-word character (/W) or the beginning (^) or end ($) of a line, and replaces it with REPLACE plus whatever was before it (${1}) and after it (${2}).

Categories