1

Currently I have to call

    $html = str_replace($search="\r\n", $replace='', $subject=$html);
    $html = str_replace($search="\n",   $replace='', $subject=$html);

to remove new line character in string $html. Is there a better/shorter way?

1

2 Answers 2

10

Try:

$html = str_replace(array("\r", "\n"), '', $html);
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you can do that at once by using an array:

$search = array("\r\n", "\n");
$result = str_replace($search, $replace='', $subject=$html);

See str_replaceDocs.

3 Comments

Yes, there is a little gap not having this as copy and paste read code. I would generally assume that gap is likely to be closed by the reader of the answer. If you run into a concrete problem dealing with it, just let me know.
The answer is misleading as some users may think - due to the way your answer is written - that str_replace() modifies the subject.
@ring0: Not for the (old) code, because subject is not passed as a variable, but only it's value as the result of the expression. As no variable is being passed, it can not be passed by reference which is the precondition for the modification you describe. Also if you're unsure, I had left a link to the function documentation. And also, yes, there is now $result to not even make some users start thinking. BTW you can improve existing answers if you feel so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.