I have a rawurlencoded string and want to get everything from the beginning of a string until a rawurlencoded newline (%0A
) or carriage return (%0D
) or the whole string if neither are in the string.
Example
From these strings:
First%20sentence.%0D%0ASecond%20Sentence.
Just%20one%20sentence.
I want to get these strings:
First%20sentence.
Just%20one%20sentence.
Basically I would like a function similar to strtok($string, "R")
, but that doesn’t work because the string is rawurlencoded and the newline and carriage return characters have been turned into a sequence of three characters each.
2
Answers
Here’s a nice way to do it:
This works in all cases:
If I were using this production, I would turn the function into a method on a class, and then I would turn those four $a, $b, $c, $d statements into unit tests.
Use the following to only removed the markers and any trailing characters IF they exist in the string.
There is no reason to create an array then extract the first element from that temporary array; just remove the unwanted substring if it actually exists.
Code: (Demo)
More concisely: (Demo)