Regex to optimize code change using Notepad++

Asked

Viewed 519 times

5

Hello, can someone please help me create a regex expression for the pattern below.

I have in my php code the following pattern: htmlspecialchars($str) I need to create an expression that replaces that for this: htmlspecialchars($str,ENT_NOQUOTES,"UTF-8")

The expression must recognize the size of $str and ignore it. After all, I hope that texts like the below have added to their end, the string: ENT_NOQUOTE,"UTF-8.

For example: Where I have:

htmlspecialchars($table->content)

should stay that way:

htmlspecialchars($table->content,ENT_NOQUOTES,"UTF-8") 

Where I have:

htmlspecialchars($_POST['title'])

should stay that way:

htmlspecialchars($_POST['title'],ENT_NOQUOTES,"UTF-8")
  • I would use in locating: (htmlspecialchars\(\$.+?(?=\))) and to replace: \1 ,ENT_NOQUOTES,"UTF-8")

  • 1

    @danieltakeshi the idea is good, but if the author already has some other who is correct it will disturb those who have already been corrected (manually), so in my answer https://answall.com/a/279926/3635 I used a group with a negation that contains the comma ([^,]), thus avoids replace being applied to strings that are already with ,ENT_NOQUOTES,"UTF-8" ;)

1 answer

8


Squeeze Ctrl+H and in the field Find what add this:

htmlspecialchars\(([^,]+?)\)

And in the field Replace with this:

htmlspecialchars\($1, ENT_NOQUOTES, "UTF-8"\)

See the example on Notepad++:

notepad++ com replace em regex


Explaining the regex:

  • The \( and the \) is to escape the parentheses that in regex are used for groups, this way will not be used so, will be as normal quotes.

  • The ([^,]+?) forms a group that searches for everything that does not have a comma, so it will avoid substituting in places where the string is already correct (htmlspecialchars($str,ENT_NOQUOTES,"UTF-8"))

  • the $1 in the field Replace with (htmlspecialchars\($1, ENT_NOQUOTES, "UTF-8"\)) serves to take the value of the group ([^,]+?) at the time of replace.

  • 3

    I love mini TL;Drs =] and even more with gifs

  • @danieltakeshi Grateful :D

  • 1

    @Guilhermenascimento, worked too much boy. Thank you very much... I do not have this competence with regex.... abs and success.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.