سؤال

I'm using XPath to get div contenteditable's content and insert it in DB. But when I output the content there are no new lines.

$divs = $xpath->query('//div');
foreach($divs as $book) {
$text = nl2br($book->textContent);
}

What am I doing wrong? Thanks in advance :)

هل كانت مفيدة؟

المحلول

$book->textContent strips all markup from the content [1], such as <div> which are naturally inserted when you hit enter on an contenteditable-Element. Therefore all this function returns is a concatenation of pure text.

I'm not sure what your goal is, but you might look into $your_DOMDocument->saveHTML($book). This will return the actual HTML of each $book including the surrounding <div>.

[1]: see the DOM Specifications

نصائح أخرى

You might be able to set a xml:whitespace="preserve" attribute on the element that has content editable and it come through using the xml api.

Failing this a solution would be to add a keypress handler to the content editable div that catches an enter and replaces it with a <br/> at the cursor position.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top