2013-07-07 1 views
0

제 젠드 프레임 워크 라이브러리의 경우이 들여 쓰기 코드가있는 뷰 헬퍼가 있습니다.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 공백입니다.

+0

당신이'' Phliplip

+0

대체 부분을 ' $ 2'으로 설정하여 정규식을 수정했습니다. 그러나 원하는 결과를 얻지 못합니다. – Phliplip

+0

@Phliplip 해결, 죄송합니다 :) 지금보십시오. – CodeAngry

관련 문제