Cita:
Iniciado por Panino5001
Puedes usar json_encode para escapar tu texto:
Código PHP:
<?php
header("Content-type:text/html;charset=utf-8");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="format-detection" content="telephone=no">
<title>test</title>
<?php
$txt='hola,
¿cómo va todo?';
?>
<script>
/*alert("<?php echo $txt; ?>");Falla: Uncaught SyntaxError: Invalid or unexpected token*/
alert(<?php echo json_encode($txt); ?>);//no falla
</script>
</head>
<body>
</body>
</html>
Gracias, parece que si funcionó asi, ahora falta probar con un texto que tiene divs y codigos de imagenes etc
Código HTML:
Ver original<?php
header("Content-type:text/html;charset=utf-8");
?>
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="format-detection" content="telephone=no"> <?php
$txt='<div>hola,
?>
$(document).ready(function() {
/*alert("<?php //echo $txt; ?>");Falla: Uncaught SyntaxError: Invalid or unexpected token*/
//alert(<?php //echo json_encode($txt); ?>);//no falla
$("#texto").val(<?php echo json_encode($txt); ?>);
});
<textarea id="texto" rows="4" cols="50">