본문으로 바로가기

textarea안에 jQuery를 통해 HTML 넣으려는데 계속 "(따옴표) 나 <, > 때문에 jQuery문법에 맞지 않다는 error가 났다


그래서 우선,


1. 정규식으로 HTML을 제거

$string = preg_replace(/<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>/i, "", $string);

로 모든 HTML을 제거 했다. 하지만 계속 파이어폭스 console 창에 

Unterminated String Literal Error라며 error발생


원인은 모르겠지만, 참고한 블로그에서는 <a>태그가 시작하고 끝나기전에 엔터구문이 있어서 라는데

나랑은 상관없었을 거 같지만,

그 블로그에서 알려준 데로 하니깐 에러 없이 동작이 잘된다.

$string=str_replace(array("\r", "\n"), array('', ''), nl2br($string)); 


* PHP & Javascript

Javascript Text: Unterminated String Literal Error

  

Happens When:  Javascript is trying to print multiple lines of text (ie, natural text from a database). 

    

What You Are Trying To Do: You want to generate some text with PHP/MYSQL that may have line breaks in it, and display that text somewhere, without javascript generating an error. 

    

My Fix:   

    

If the text is entered in as natural text (ie, has line breaks in it, like regular paragraphs so often do), this will break your javascript- because javascript only wants to deal with text on a non-breaking line. If your text has line breaks in it, you will get an error from the javascript about 'unterminated string literal'. 

The fix is simple- you break your text into chunks at the breaks and have javascript print those chunks.

This example is for populating a textbox element- but can be used in any circumstance.

Take my code and modify it for your purposes. Here's what it does:


1. Take your text variable ($xData), and convert those '\n' line breaks to '<br />' using the nl2br PHP command.

2. Explode your translated text into an array at the breaks ('<br />');

3. So now you have an array with all the elements.

4. Use a For Each loop to echo out your javascript in PHP- one line for each value in the array, and add a newline character in the javascript. The javascript should stack the variables (ie, add them to the previous value with a javascript line break). Here's the code:


$xData=str_replace(array("\r", "\n"), array('', ''), nl2br($xData));
$this_data=explode("
", $xData); echo "document.getElementById('textb').value='';\n"; foreach ($this_data as $key=>$value) { echo "document.getElementById('textbox1').value=document.getElementById('textbox1).value+'".$value."\\n'; \n"; } unset($this_data);


5. I unset the variable at the end, because I am looping through a number of different variables, and if you don't, then the last vars opened will be added to the text.


참고 블로그 : 

http://moozuknet.blog.me/60072394366

http://blog.naver.com/0131v?Redirect=Log&logNo=110167638207