Viewed   80 times

We have written a small PHP Hook for our billing system that opens a new support ticket with us when an order is placed. It works except that for the "Open Ticket" API function, it takes a string for the message, but we cannot figure out how to put carriage returns in it.

I have tried

<p>, <br>, n, rn, etc.

As it appears to just be completely plain text though, all of these are just being read verbatim rather than made into carriage returns.

Does anyone have any thoughts on how this could be done? http://docs.whmcs.com/API:Open_Ticket

 Answers

4

Carriage return is "r". Mind the double quotes!

I think you want "rn" btw to put a line break in your text so it will be rendered correctly in different operating systems.

  • Mac: r
  • Linux/Unix: n
  • Windows: rn
Saturday, December 3, 2022
 
1

I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:

$ cat infile
hello
goodbye

$ cat infile | od -c
0000000   h   e   l   l   o  r  n   g   o   o   d   b   y   e  n
0000017

dos2unix is the way to go if it's installed on your system:

$ cat infile | dos2unix -U | od -c
0000000   h   e   l   l   o  n   g   o   o   d   b   y   e  n
0000016

If for some reason dos2unix is not available to you, then sed will do it:

$ cat infile | sed 's/r$//' | od -c
0000000   h   e   l   l   o  n   g   o   o   d   b   y   e  n
0000016

If for some reason sed is not available to you, then ed will do it, in a complicated way:

$ echo ',s/rn/n/
> w !cat
> Q' | ed infile 2>/dev/null | od -c
0000000   h   e   l   l   o  n   g   o   o   d   b   y   e  n
0000016

If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)

Thursday, December 15, 2022
 
1

Do you want the carriage return / newline, or don't you? Your title says that you don't, your code is explicitly adding carriage returns when the string has a newline. If you want to get rid of both, use String.replaceAll(), which takes a regex:

public static void main(String[] argv)
throws Exception
{
    String s1 = "thisrnis a test";
    String s2 = s1.replaceAll("[nr]", "");
    System.out.println(s2);
} 

This example finds any occurrence of the characters, and deletes them. You probably want to look for the sequence of characters and replace with a space, but I'll leave that up to you: look at the doc for java.util.regex.Pattern.

And I suspect that the "box" is some other character, not a return or newline.

Monday, August 1, 2022
 
yahh
 
2

In my understanding it does

fprintf(fid, '%s', strrep(message, sprintf('n'), sprintf('rn'))

If you do

fprintf(fid, '%srn', message)

you're only adding one carriage return and a newline at the very end of your message, which is after "worldn".The newline character between "hello" and "world" remains without carriage return.

So in your fprintf your message is "hellonworldnrn", where it should be "hellornworldrn"

You can check this by reading the output file in bytes, knowing that n will be a 10 as uint8 and r a 13:

>> fid = fopen('test.txt','wt');
>> fprintf(fid, 'hellonworldn');
>> fclose(fid);
>> fid = fopen('test.txt','r');
>> bytes = fread(fid, Inf, 'uint8')'

bytes =

   104   101   108   108   111    13    10   119   111   114   108   100    13    10
Friday, October 7, 2022
4

The following function overwrite transforms its argument such that after each carriage return r the beginning of the string is actually overwritten:

overwrite() {
    local segment result=
    while IFS= read -rd $'r' segment; do
       result="$segment${result:${#segment}}"
    done < <(printf '%sr' "$@")
    printf %s "$result"
}

Example

$ overwrite $'abcdefr0123rxy'
xy23ef

Note that the printed string is actually xy23ef, unlike echo $'abcdefr0123rxy' which only seems to print the same string, but still prints r which is then interpreted by your terminal such that the result looks the same. You can confirm this with hexdump:

$ echo $'abcdefr0123rxy' | hexdump -c
0000000   a   b   c   d   e   f  r   0   1   2   3  r   x   y  n
000000f
$ overwrite $'abcdefr0123rxy' | hexdump -c
0000000   x   y   2   3   e   f
0000006

The function overwrite also supports overwriting by arguments instead of r-delimited segments:

$ overwrite abcdef 0123 xy
xy23ef

To convert variables in-place, use a subshell: myvar=$(overwrite "$myvar")

Friday, September 2, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :