// fin de ligne define('EOL', chr(10)); // récupération et correction des fins de lignes et convertion en tableau $words = explode( EOL, str_replace(EOL.EOL, EOL, str_replace(chr(13), EOL, $_POST['value']); )); // trim et suppréssion, si vide, des lignes foreach($words as $k => &$v){ $v = trim($v); if(empty($v)) { unset($words[$k]); } } print('<pre>'); print_r($words); print('</pre>');
Mais c'est pas ça, ton code renvois un array type
Array
(
[0] => p
[1] => o
[2] => p
)
Alors que moi je veux qu'il se transforme en
array('p', 'o', 'p');
$tbl = array(0,1,2,3,4,5,6,6,7,8,9); print_r($tbl); $t=array(); for($i=0;$i<10;$i++){ $t[$i]=$i; } print_r($t);
donc en gros qu'il renvois du code PHP.
$concatenationdeV =""; // trim et suppréssion, si vide, des lignes foreach($words as $k => &$v){ $concatenationdeV .= "'".trim($v) . "',"; if(empty($v)){ unset($words[$k]); } } $maStringArr = "array(".substr($concatenationdeV ,0,-1).")"; Echo $maStringArr;
ton code me renvois une erreurEt je pense que tu devrais pouvoir la trouver assez facilement....
Parse error: syntax error, unexpected '.' ..... at line : xxx
Oui c'est bien ça, je cherche avoir une chaine de cractères qui contient le mot arrayAh ben ... expliqué comme ça ... ça devient tout de suite plus facile à comprendre.. dommage que tu ne l'ai pas formulé directement comme ça dans ta question.. ça aurait été plus simple... non ?
<?php // fin de ligne define('EOL', chr(10)); // récupération et correction des fins de lignes et convertion en tableau $words = explode( EOL, str_replace(EOL.EOL, EOL, str_replace(chr(13), EOL, isset($_POST['value']) ? $_POST['value'] : ''); )); // trim et suppréssion, si vide, des lignes foreach($words as $k => &$v){ $v = trim($v); if(empty($v)) { unset($words[$k]); } } if(count($words) == 0) { echo "array();"; } else { echo "array('", implode("','", $words),"');"; } ?>
echo json_encode($words);
$words = explode( EOL, str_replace(EOL.EOL, EOL,str_replace(chr(13), EOL, isset($_POST['value']) ? $_POST['value'] : '')));
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question<?php /* REQUIRED */ define('EOL', chr(10)); /* explode_words sépares les mots d'une phrase, ainsi que la ponctuation ($replaceAll=false) paramètres : $str string chaine contenant la phrase à découper $replaceAll bool remplace (true) ou non (false) les ponctuations et certains autres caractères retour : array, tableau mono-dimensionnel contenant tout les mots de la phrase. */ function explode_words($str, $replaceAll = true) { $str = str_replace(array(chr(13), chr(32), EOL.EOL), EOL, $str); if($replaceAll){ $str = str_replace(array('! ','? ','. ','; ',', ',': ','/','\\','|','\'',''','"','{','}','(',')','[',']',' '), EOL, ' '.$str.' '); } $words = explode( EOL, $str ); foreach($words as $k => &$v) { $v = trim(addslashes($v)); if(empty($v)){ unset($words[$k]); } } return array_values($words); } /* words_to_phparray convertis un tableau en code php array(value,...); ou array(key=>value,...) ($includeKeys=true) paramètres : $words array tableau mono/multi dimensionnel $includeKeys bool inscrit les clefs d'association dans le retour retour : string, représentation chaine d'un tableau PHP */ function words_to_phparray(array $words,$includeKeys=false) { if(count($words)==0){ return "array();"; } if($includeKeys){ $r = 'array('.EOL; $c = 0; foreach($words as $k => $v){ if($c++>0){ $r.= ','.EOL; } $r.= ' '; $r.= preg_match('/^\d{1,3}$/',$k) ? $k : "'$k'"; $r.= ' => '; if(is_array($v)){ $r.= words_to_phparray($v, $includeKeys); } else { $r.= preg_match('/^\d+([.]{1}\d+){0,1}$/',$v) ? $v : "'$v'"; } } $r.= EOL.');'.EOL; return $r; } else { return "array('".implode("','", $words)."');".EOL; } } /* words_to_json convertis un tableau de mots en chaine tableau JSON paramètres : $words array tableau de mot retour : string, représentation chaine d'un tableau JSON (utilisable avec PHP json_decode ou Javascript ParseJSON */ function words_to_json(array $words) { return json_encode( $words, JSON_NUMERIC_CHECK); } /*------------------------------ TESTS ------------------------------*/ /*------------------------------ TESTS ------------------------------*/ /*------------------------------ TESTS ------------------------------*/ $str = "Pi est égal à 3.14159 mais avant le 21eme siecle nous utilisions uniquement 3 voir 4."; print('<h3>$str</h3><pre>'.$str.'</pre><hr/>'); $words = explode_words($str); print('<h3>$words by explode_words($str)</h3><pre>'); print_r($words); print('</pre><hr/>'); $wordsNRA = explode_words($str, false); print('<h3>$wordsNRA by explode_words($str, false)</h3><pre>'); print_r($wordsNRA); print('</pre><hr/>'); $phparray = words_to_phparray($words); print('<h3>$phparray by words_to_array($words)</h3><pre>'.$phparray.'</pre><hr/>'); $phparrayKeys = words_to_phparray($words, true); print('<h3>$phparrayKeys by words_to_array($words, true)</h3><pre>'.$phparrayKeys.'</pre><hr/>'); eval( '$interprete = '.$phparray ); print('<h3>$interprete by eval($phparray)</h3><pre>'); print_r($interprete); print('</pre><hr/>'); $json = words_to_json($words); print('<h3>$json by words_to_json($words)</h3><pre>'.$json.'</pre><hr/>'); $object = json_decode($json); print('<h3>$object by json_decode($json)</h3><pre>'); print_r($object); print('</pre><hr/>'); /*------------------------------ TESTS ------------------------------*/ /*------------------------------ TESTS ------------------------------*/ /*------------------------------ TESTS ------------------------------*/ ?>