Split(php explode) by new line in server connect

Hi,

I’ve got a string that I want to explode into an array.
The raw data comes with a new line character as part of the string \r\n
I’ve tried several ways of escaping the characters but no luck yet.

Any ideas?

Hey Jon,

How about a custom formatter using php explode?

https://www.php.net/explode

1 Like

You can use the splitter formatter to split the lines and then loop through those and split on separator

1 Like

I thought of that, but I noticed that the split formatter in SC(php) already uses explode so I thought maybe I wasn’t correctly escaping characters or at least not how SC would expect. I didn’t want to reinvent the wheel.

function formatter_split($val, $delimiter) {
    return explode(strval($delimiter), strval($val));
}

Can you elaborate on that approach?

I’ve got the following string(It’s longer but for the example purpose I’ll make it short).

“0005AD76BD555C1D6D771DE417A4B87E4B4:4\r\n000A8DAE4228F821FB418F59826079BF368:3\r\n000DD7F2A1C68A35673713783CA390C9E93:630”

I need to split on \r\n and I’ve tried every kind of combination I thought of but I can’t seem to escape correctly " \ " with the SC .split() function. I also tried to splitting on just \n and \r to no avail.

Well you should be able to split on \n\r

No need to escape the \ character

Some progress.

I literally split by a new line. Literally.

image

image

Now I need to be able to target \r which I can’t seem to find the way to do it.

1 Like

While I wait to see if it can be done from Core I created a custom formatter for this.

I used preg_split instead of the regular explode as I want to catch all cases (\n and \r\n). @mebeingken

function formatter_splitme($val) {
    return preg_split('/\n|\r\n?/', $val);
}

image

1 Like

The problem with \r\n using the UI are fixed in Wappler. Also a custom formatter can be used when you want to use a regexp for the split, as you already replied yourself.

1 Like

Can someone please point me to the documentation for custom formatters?

Thanks.