제 젠드 프레임 워크 라이브러리의 경우이 들여 쓰기 코드가있는 뷰 헬퍼가 있습니다.HTML 콘텐츠의 textarea 태그 내의 앞 공백을 제거하십시오.
내가
$content = 'This is some text' . PHP_EOL;
$content .= '<div>' . PHP_EOL;
$content .= ' Text inside div, indented' . PHP_EOL;
$content .= '</div>' . PHP_EOL;
$content .= '<form>' . PHP_EOL;
$content .= ' <ul>' . PHP_EOL;
$content .= ' <li>' . PHP_EOL;
$content .= ' <label>inputfield</label>' . PHP_EOL;
$content .= ' <input type="text" />' . PHP_EOL;
$content .= ' </li>' . PHP_EOL;
$content .= ' <li>' . PHP_EOL;
$content .= ' <textarea cols="80" rows="10">' . PHP_EOL;
$content .= 'content of text area' . PHP_EOL;
$content .= ' this line is intentionally indented with 3 whitespaces' . PHP_EOL;
$content .= 'content of text area' . PHP_EOL;
$content .= ' </textarea>' . PHP_EOL;
$content .= ' </li>' . PHP_EOL;
$content .= ' </ul>' . PHP_EOL;
$content .= '</form>' . PHP_EOL;
$content .= 'The end';
echo $this->view->indent (6, $content);
내가하고 싶은 무엇 ..
<?php
class My_View_Helper_Indent
{
function indent($indent, $string, $space = ' ')
{
if($string == '') {
return '';
}
$indent = str_repeat($space, $indent);
$content = $indent . str_replace("\n", "\n" . $indent, rtrim($string)) . PHP_EOL;
// BEGIN SOLUTION TO PROBLEM
// Based on user CodeAngry's answer
$callback = function($matches) use ($indent) {
$matches[2] = str_replace("\n" . $indent, "\n", $matches[2]);
return '<textarea' . $matches[1] . '>' . $matches[2] . '</textarea>';
};
$content = preg_replace_callback('~<textarea(.*?)>(.*?)</textarea>~si', $callback, $content);
// END
return $content;
}
}
그리고 다음 테스트 코드와 아래 코드에서 솔루션을 추가 한, X의 공백 문자를 제거하는 것입니다 textarea 태그가있는 행에서 가져옵니다. 여기서 X는 코드가 들여 쓰여진 공백의 수와 일치합니다. 위 예제에서 6 공백입니다.
당신이''
아니요, 내 들여 쓰기 기능에 의해 추가 된 공백 만 .. 사용자가 의도적으로 양식을 제출하기 전에 텍스트 영역의 줄을 들여 쓰기하고 양식이 오류로 인해 다시 표시되었다고 가정합니다. 그런 다음 의도적으로 추가 된 공백을 원하지 않습니다. 사라합니다. – Phliplip