par
Ajoloca » 22 janv. 2007, 13:50
Bonjour,
Personnellement, je préfère les solutions génériques (utilisables par tout)
Bien que je n'ai pas compris l'utilité de l'écrire sur plusieurs enregistrements,
voici une solution qui fonctionne quel que soit le système.
La chaine contient des \n\r des \n et \r
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/>
<title>ess.php</title>
</head>
<body>
<?php
function my_nl2br($str){
$nl = Array("\n\r", "\n", "\r");
return str_replace($nl, '<br />', $str);
}
$chaine="Première ligne\nune ligne vide\n\nune ligne\n\rune autre\ret la dernière";
$chaine_br = my_nl2br($chaine);
echo htmlentities($chaine_br);
$tab = explode('<br />', $chaine_br);
var_dump($tab);
echo '<pre>';
print_r($tab);
echo '</pre>';
?>
</body>
</html>
et le résultat est
Code : Tout sélectionner
Première ligne<br />une ligne vide<br /><br />une ligne<br />une autre<br />et la dernière
array
0 => string 'Première ligne' (length=21)
1 => string 'une ligne vide' (length=14)
2 => string '' (length=0)
3 => string 'une ligne' (length=9)
4 => string 'une autre' (length=9)
5 => string 'et la dernière' (length=21)
Array
(
[0] => Première ligne
[1] => une ligne vide
[2] =>
[3] => une ligne
[4] => une autre
[5] => et la dernière
)
Bonjour,
Personnellement, je préfère les solutions génériques (utilisables par tout)
Bien que je n'ai pas compris l'utilité de l'écrire sur plusieurs enregistrements,
voici une solution qui fonctionne quel que soit le système.
La chaine contient des \n\r des \n et \r[php]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/>
<title>ess.php</title>
</head>
<body>
<?php
function my_nl2br($str){
$nl = Array("\n\r", "\n", "\r");
return str_replace($nl, '<br />', $str);
}
$chaine="Première ligne\nune ligne vide\n\nune ligne\n\rune autre\ret la dernière";
$chaine_br = my_nl2br($chaine);
echo htmlentities($chaine_br);
$tab = explode('<br />', $chaine_br);
var_dump($tab);
echo '<pre>';
print_r($tab);
echo '</pre>';
?>
</body>
</html>[/php]
et le résultat est[code]Première ligne<br />une ligne vide<br /><br />une ligne<br />une autre<br />et la dernière
array
0 => string 'Première ligne' (length=21)
1 => string 'une ligne vide' (length=14)
2 => string '' (length=0)
3 => string 'une ligne' (length=9)
4 => string 'une autre' (length=9)
5 => string 'et la dernière' (length=21)
Array
(
[0] => Première ligne
[1] => une ligne vide
[2] =>
[3] => une ligne
[4] => une autre
[5] => et la dernière
)
[/code]