I have a lot of shortcodes left in my posts from a non-working plugin of this type:
[download#434]

or [download#434#nohits]

or [download#68#size]

How to use regular expressions to overwrite all of them to the same type:
[download id="68"] ?

    2 answers 2

     $result = preg_replace("/\[download#([0-9]+).*?]/ium", "[download id=\"$1\"]", $string); 

    where $ string is the string to be processed.

    • I do not understand why both of the corresponding m modifier were stuffed into the regular season? P, S. In such a regular schedule, neither u nor i are needed, and the more 'm'. - Visman
    • @Visman, thanks, totally agree with you. Are there any disadvantages when using them in this context? - Zhukov Roman
    • i and u may well slow down the processing of preg_replace (strongly depends on the version of php), m runs idle and does not cause any harm until someone looks at your regulars and does the same, but using ^ , $ . - Visman Nov.
     $string = '[download#688#nohits] as [download#34]'; $pattern = '/\[(\w+)#(\d+).*?\]/im'; $replacement = '[$1 id="$2"]'; echo preg_replace($pattern, $replacement, $string); 
    • Bad expression. Test on this line: '[download # 434 # 23] ab [download # 4]' - Zhukov Roman
    • @ZhukovRoman, corrected the answer. - Klym Nov.
    • And now on this: '[upload # 688 # nohits] as [download # 34]'. - Zhukov Roman Nov.
    • @ZhukovRoman, tested, everything works. - Klym
    • Well, if you don’t assume that the upload tag belongs to another plugin and you don’t need to change it, then yes :) - Zhukov Roman