
nl2br 함수는 HTML 엔터를 줄바꿈으로 바꾸는 함수입니다. 이 함수는 PHP에서 사용할 수 있습니다.
nl2br 함수를 모든 문자열에 적용하려면, PHP의 모든 문자열을 이 함수에 적용할 수 있습니다. 예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$text = "HellonWorld";
echo nl2br($text); // Hello
World
$text = "HellonWorldnPython";
echo nl2br($text); // Hello
World
Python
하지만, 위의 예제에서는 PHP의 문자열에서 \n을 엔터로 인식합니다. 만약, PHP의 문자열에서 \n을 엔터로 인식하지 않는다면, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$text = "HellonWorld";
$text = str_replace("n", "
", $text);
echo $text; // Hello
World
$text = "HellonWorldnPython";
$text = str_replace("n", "
", $text);
echo $text; // Hello
World
Python
또는, PHP의 문자열에서 \n을 엔터로 인식하고 싶지 않다면, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$text = "HellonWorld";
$text = str_replace("n", "\n", $text);
echo nl2br($text); // Hellon
World
$text = "HellonWorldnPython";
$text = str_replace("n", "\n", $text);
echo nl2br($text); // Hellon
Worldn
Python
위의 예제에서, str_replace 함수를 사용하여 \n을 \\n으로 바꾸고, nl2br 함수를 사용하여 HTML 엔터를 줄바꿈으로 바꾸었습니다.
2025-05-15 19:09