"replace" keyword modifier


(Oleg Voronin) #1

Struggling a bit with this one; I'm after correct syntax for throwing out line breaks with an empty string. The replacement doesn't seem to understand "\r\n" literal, and my attempts at using valid [php] regular expression syntax have failed silently. (NB: apparently not all valid regexps work in Squiz)

 

Can someone point me in the right direction?


(Nic Hubbard) #2

Struggling a bit with this one; I'm after correct syntax for throwing out line breaks with an empty string. The replacement doesn't seem to understand "\r\n" literal, and my attempts at using valid [php] regular expression syntax have failed silently. (NB: apparently not all valid regexps work in Squiz)

 

The problem with the replace modifier is, even though it uses the php preg_replace() function, the argument that you use actually gets stripped earlier in the code. So, when it gets to that function, what you entered probably isn't even there anymore.

 

I tried:

%globals_asset_contents_raw:7750^json_encode^replace:(\r?\n){2,}:---%

This returns nothing. But, when looking into the source of that modifier, and echoing out what the $toReplace and $replaceWith vars have I realize that it left me with the following regex:


(\n)+

Not exactly what I started with. So, somewhere earlier in the code the regex is getting stripped from the keyword.

 

I am going to submit a bug report for this.


(Oleg Voronin) #3

Thanks for helping preserve my sanity! I was really unable to get replacement working for anything other than basic strings - for instance, I couldn't figure out a way to replace "<br/>" tags (as a workaround, I was considering %...^nl2br^replace:<br/>:% instead of the above)


(Nic Hubbard) #4

I couldn't figure out a way to replace "<br/>" tags (as a workaround, I was considering %...^nl2br^replace:<br/>:% instead of the above)

 

You can't. Somewhere before the modifiers get replaced that regex string gets stripped. Not sure if there is a list of characters that are not allowed. But I put in a bug report for this so I hope it will get fixed.


(Bart Banda) #5

Until the bug report is actioned, here are some workarounds that have worked for me:

^json_encode^urlencode^replace:5Cr:20^replace:5Cn:20^urldecode%

^urlencode^replace:0D:20^replace:0A:20^urldecode%

Basically just replaces the line break with a space. Not an ideal solution, but pretty close and might be just enough for what you need. Hope that helps.

 


(Oleg Voronin) #6

 

Until the bug report is actioned, here are some workarounds that have worked for me:

^json_encode^urlencode^replace:5Cr:20^replace:5Cn:20^urldecode%

^urlencode^replace:0D:20^replace:0A:20^urldecode%

Basically just replaces the line break with a space. Not an ideal solution, but pretty close and might be just enough for what you need. Hope that helps.

 

 

 

Great workaround, thank you!