Regex to optimize code change using Notepad ++

5

Hello, someone can kindly 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 this with this: htmlspecialchars($str,ENT_NOQUOTES,"UTF-8")

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

For example: Where I have:

htmlspecialchars($table->content)

should look like this:

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

Where I have:

htmlspecialchars($_POST['title'])

should look like this:

htmlspecialchars($_POST['title'],ENT_NOQUOTES,"UTF-8")
    
asked by anonymous 01.03.2018 / 20:15

1 answer

8

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

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

And in the Replace with field this:

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

See the example in notepad ++:

  

Explainingregex:

  • The\(and\)istoescapetheparenthesesthatinregexareusedforgroups,thustheywillnotbeusedlikethis,theywillbelikenormalquotationmarks.

  • The([^,]+?)formsagroupthatlooksforeverythingthathasnocomma,soitwillavoiddoingthesubstitutioninplaceswherethestringisalreadycorrect(htmlspecialchars($str,ENT_NOQUOTES,"UTF-8") )

01.03.2018 / 21:20