couper un texte en php
Dernière réponse : dans Programmation
Salutation, voilà après de multiples recherche je m'en remets à vous.
j'ai une variable $texte que je chope de ma bdd. Je veux créer un chapeau pour ma news du style:
"Nouveau jeux vidéo très interessant à ne surtout pas manquer ... >>> lire la suite"
à partir du texte de la news qui est biensur beaucoup plus long. Comment je peux faire pour couper cette chaine de caractère à 250 caractères par exemple sans qu'il me coupe un mot en plein milieu.
Merci d'avance!
j'ai une variable $texte que je chope de ma bdd. Je veux créer un chapeau pour ma news du style:
"Nouveau jeux vidéo très interessant à ne surtout pas manquer ... >>> lire la suite"
à partir du texte de la news qui est biensur beaucoup plus long. Comment je peux faire pour couper cette chaine de caractère à 250 caractères par exemple sans qu'il me coupe un mot en plein milieu.
Merci d'avance!
Autres pages sur : couper texte php
Lassé par la pub ? Créez un compte
<?php
$sText = $texte;
$iMaxLength = 125;
$sMessage = "...";
function wordCut($sText, $iMaxLength, $sMessage)
{
if (strlen($sText) > $iMaxLength)
{
$sString = wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
$asExplodedString = explode('[cut]', $sString);
echo $sCutText = $asExplodedString[0];
$sReturn = $sCutText.$sMessage;
}
else
{
$sReturn = $sText;
}
return $sReturn;
}
?>
J'ai mis ca, l'erreur est :
Fatal error: Cannot redeclare wordcut() (previously declared in /home.11/c/l/a/clantca/www/home.php:163) in /home.11/c/l/a/clantca/www/home.php on line 163
$sText = $texte;
$iMaxLength = 125;
$sMessage = "...";
function wordCut($sText, $iMaxLength, $sMessage)
{
if (strlen($sText) > $iMaxLength)
{
$sString = wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
$asExplodedString = explode('[cut]', $sString);
echo $sCutText = $asExplodedString[0];
$sReturn = $sCutText.$sMessage;
}
else
{
$sReturn = $sText;
}
return $sReturn;
}
?>
J'ai mis ca, l'erreur est :
Fatal error: Cannot redeclare wordcut() (previously declared in /home.11/c/l/a/clantca/www/home.php:163) in /home.11/c/l/a/clantca/www/home.php on line 163
j'ai ca:
<?php
include("dbdata.php");
//requete sql
$id_result = mysql_query ("SELECT * FROM `news` ORDER BY `id_news` DESC LIMIT 6",$id_connex) or die("mauvaise requete");
$num_result=mysql_num_rows($id_result);
while($tabnews = mysql_fetch_array($id_result,MYSQL_ASSOC))
{
$debcom=$tabnews['id_news'];
$res=mysql_query("SELECT * FROM commentaires WHERE id_news=$debcom");
$nb_news=mysql_num_rows($res);
$titre=stripslashes($tabnews["titre"]);
$texte=stripslashes($tabnews["texte"]);
?>
ici le tableau de ma news avec la fonction wordcut() etc (copier collé de plus haut) qui me ressort mon texte
<?
}
?>
et la fin de ma boucle
<?php
include("dbdata.php");
//requete sql
$id_result = mysql_query ("SELECT * FROM `news` ORDER BY `id_news` DESC LIMIT 6",$id_connex) or die("mauvaise requete");
$num_result=mysql_num_rows($id_result);
while($tabnews = mysql_fetch_array($id_result,MYSQL_ASSOC))
{
$debcom=$tabnews['id_news'];
$res=mysql_query("SELECT * FROM commentaires WHERE id_news=$debcom");
$nb_news=mysql_num_rows($res);
$titre=stripslashes($tabnews["titre"]);
$texte=stripslashes($tabnews["texte"]);
?>
ici le tableau de ma news avec la fonction wordcut() etc (copier collé de plus haut) qui me ressort mon texte
<?
}
?>
et la fin de ma boucle
à présent:
en tout début de page j'ai définit :
<?php
/**
* function wordCut($sText, $iMaxLength, $sMessage)
*
* + cuts an wordt after $iMaxLength characters
*
* @param string $sText the text to cut
* @param integer $iMaxLength the text's maximum length
* @param string $sMessage piece of text which is added to the cut text, e.g. '...read more'
*
* @returns string
**/
function wordCut($sText, $iMaxLength, $sMessage)
{
if (strlen($sText) > $iMaxLength)
{
$sString = wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
$asExplodedString = explode('[cut]', $sString);
echo $sCutText = $asExplodedString[0];
$sReturn = $sCutText.$sMessage;
}
else
{
$sReturn = $sText;
}
return $sReturn;
}
?>
au niveau de ma boucle j'ai ajouté:
$sText = $texte;
$iMaxLength = 10;
$sMessage = "...";
et dans ma boucle dans mon tableau news:
<? wordCut("$sText", "$iMaxLength", "$sMessage"); ?>
résultat:
j'ai un mot dans ma première news: Va
et 2 dans la deuxième: Est -
et rien d'autre
en tout début de page j'ai définit :
<?php
/**
* function wordCut($sText, $iMaxLength, $sMessage)
*
* + cuts an wordt after $iMaxLength characters
*
* @param string $sText the text to cut
* @param integer $iMaxLength the text's maximum length
* @param string $sMessage piece of text which is added to the cut text, e.g. '...read more'
*
* @returns string
**/
function wordCut($sText, $iMaxLength, $sMessage)
{
if (strlen($sText) > $iMaxLength)
{
$sString = wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
$asExplodedString = explode('[cut]', $sString);
echo $sCutText = $asExplodedString[0];
$sReturn = $sCutText.$sMessage;
}
else
{
$sReturn = $sText;
}
return $sReturn;
}
?>
au niveau de ma boucle j'ai ajouté:
$sText = $texte;
$iMaxLength = 10;
$sMessage = "...";
et dans ma boucle dans mon tableau news:
<? wordCut("$sText", "$iMaxLength", "$sMessage"); ?>
résultat:
j'ai un mot dans ma première news: Va
et 2 dans la deuxième: Est -
et rien d'autre
Bon pour ceux que ca interesse au final:
En début de page j'ai définit la fonction:
<?php
/**
* function wordCut($sText, $iMaxLength, $sMessage)
*
* + cuts an wordt after $iMaxLength characters
*
* @param string $sText the text to cut
* @param integer $iMaxLength the text's maximum length
* @param string $sMessage piece of text which is added to the cut text, e.g. '...read more'
*
* @returns string
**/
function wordCut($sText, $iMaxLength, $sMessage)
{
if (strlen($sText) > $iMaxLength)
{
$sString = wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
$asExplodedString = explode('[cut]', $sString);
echo $sCutText = $asExplodedString[0];
echo "".$sCutText."".$sMessage."";
}
else
{
echo "".$sText."";
}
}
?>
et dans ma boucle:
$sText = $texte;
$iMaxLength = 120;
$sMessage = "...";
<? wordCut("$sText", "$iMaxLength", "$sMessage"); ?> (à l'endroit ou je veux le texte dans ma news)
Merci Okinou
En début de page j'ai définit la fonction:
<?php
/**
* function wordCut($sText, $iMaxLength, $sMessage)
*
* + cuts an wordt after $iMaxLength characters
*
* @param string $sText the text to cut
* @param integer $iMaxLength the text's maximum length
* @param string $sMessage piece of text which is added to the cut text, e.g. '...read more'
*
* @returns string
**/
function wordCut($sText, $iMaxLength, $sMessage)
{
if (strlen($sText) > $iMaxLength)
{
$sString = wordwrap($sText, ($iMaxLength-strlen($sMessage)), '[cut]', 1);
$asExplodedString = explode('[cut]', $sString);
echo $sCutText = $asExplodedString[0];
echo "".$sCutText."".$sMessage."";
}
else
{
echo "".$sText."";
}
}
?>
et dans ma boucle:
$sText = $texte;
$iMaxLength = 120;
$sMessage = "...";
<? wordCut("$sText", "$iMaxLength", "$sMessage"); ?> (à l'endroit ou je veux le texte dans ma news)
Merci Okinou
Lassé par la pub ? Créez un compte
- Contenus similaires :
Tags :
- ForumCouper un texte php
- ForumFonction couper texte php
- ForumEcrire dans un fichier texte en php
- ForumApostrophe champ texte php
- ForumLes zones de texte php
- ForumEditeur de texte php html
- ForumLire fichier texte en php
- ForumExtrait texte php
- ForumZone texte en php
- ForumZone de texte en php
- Voir plus