<?php
//fonction limitation de mots
function debutchaine($chaine, $nbmots) { // 1er argument : chaîne - 2e argument : nombre de mots
$chaine = preg_replace('!<br.*>!iU', "", $chaine); // remplacement des BR par des espaces
$chaine = strip_tags($chaine);
$chaine = preg_replace('/\s\s+/', ' ', $chaine); // retrait des espaces inutiles
$tab = explode(" ",$chaine);
if (count($tab) <= $nbmots) {
$affiche = $chaine;
}
else {
$affiche = "$tab[0]";
for ($i=1; $i<$nbmots; $i++) {
$affiche .= " $tab[$i]";
}
}
if (count($tab) > $nbmots ){
$affiche .= '... <a href="http://www.adresse.fr"><b>Lire la suite</b></a>';
}
return $affiche;
}
$mots_complets = 'salut sa va bien je suis un super pote a toi on rigole tou le temp c tro dorle comme on ce marre c bien le stage ouai sa va encore c pa mal mais bon c pa tt le temp marran oui mai comme dans la vrai vie tu sais on fai pa tout le temps ce quon veu sa serait telllement rigolo sinon ahahahahahahahahahaha bebebebebebebebeeb cececececececececececec'; //data événement
$nb_mots = 50;
$mot_courts = debutchaine($mots_complets, $nb_mots);
echo $mot_courts;
?>
J'ai donc repris ce code et me suis arranger pour le faire en caractères.$nb_mots = substr($mots_complets, 0, 70);
Merci pour votre aide
$max=20;
if(strlen($chaine)>=$max){$chaine=substr($chaine,0,$max);
$espace=strrpos($chaine," ");
$chaine=substr($chaine,0,$espace)."..."; }
echo $chaine;
Aucun mot ne sera coupé au milieu et il y aura ... après 20 caractères.<?php
//fonction limitation de mots
function debutchaine($chaine, $nbmots) { // 1er argument : chaîne - 2e argument : nombre de mots
$max=50;
if(strlen($chaine)>=$max){$chaine=substr($chaine,0,$max);
$espace=strrpos($chaine," ");
$chaine=substr($chaine,0,$espace).'... <a href="http://www.site.fr"><b>Lire la suite</b></a>'; }
//echo $chaine;
$tab = explode(" ",$chaine);
if (count($tab) <= $nbmots) {
$affiche = $chaine;
}
else {
$affiche = "$tab[0]";
for ($i=1; $i<$nbmots; $i++) {
$affiche .= " $tab[$i]";
}
}
return $affiche;
}
$mots_complets = 'salut sa va bien je suis un super pote a toi on rigole tou le temp c tro dorle comme on ce marre c bien le stage ouai sa va encore c pa mal mais bon c pa tt le temp marran oui mai comme dans la vrai vie tu sais on fai pa tout le temps ce quon veu sa serait telllement rigolo sinon ahahahahahahahahahaha bebebebebebebebeeb cececececececececececec'; //data événement
$nb_mots = 50;
$mot_courts = debutchaine($mots_complets, $nb_mots);
echo $mot_courts;
?>
<?php
// Connexion au serveur
include 'admin/connectbddwp.php';
//Je sélectionne toutes les catégories de la table
$sql = "SELECT post_content FROM wp_posts WHERE id=1";
$enreg = mysql_query($sql) or die(mysql_error());
while ($rec = mysql_fetch_array($enreg)){
$mots_complets = $rec['post_content'];
//fonction limitation de mots
function debutchaine($chaine, $nbmots) { // 1er argument : chaîne - 2e argument : nombre de mots
$max=40;
if(strlen($chaine)>=$max){$chaine=substr($chaine,0,$max);
$espace=strrpos($chaine," ");
$chaine=substr($chaine,0,$espace).'... <a href="http://www.iltc.fr"><b>Lire la suite</b></a>'; }
//echo $chaine;
$tab = explode(" ",$chaine);
if (count($tab) <= $nbmots) {
$affiche = $chaine;
}
else {
$affiche = "$tab[0]";
for ($i=1; $i<$nbmots; $i++) {
$affiche .= " $tab[$i]";
}
}
return $affiche;
}
$nb_mots = 10;
$mot_courts = debutchaine($mots_complets, $nb_mots);
echo $mot_courts;
}
?>
debutchaine($mots_complets['post_content'], $nb_mots)
mysql_insert_id() convertit le type de valeur retourné par la fonction C de MySQL C mysql_insert_id() en type PHP long (appelé int en PHP). Si votre colonne de type AUTO_INCREMENT est une colonne de type BIGINT, la valeur retournée par mysql_insert_id() sera incorrecte. À la place, utilisez la fonction interne MySQL LAST_INSERT_ID() dans une requête SQL.