note 110147 added to function.preg-match-all
| From: | fab | Date: | Mon, 24 Sep 2012 09:14:48 +0000 |
| Subject: | note 110147 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190988@lists.php.net to get a copy of this message | ||
Here is a function that replaces all occurrences of a number in a string by the number--
<?php
function decremente_chaine($chaine)
{
//récupérer toutes les occurrences de nombres et leurs indices
preg_match_all("/[0-9]+/",$chaine,$out,PREG_OFFSET_CAPTURE);
//parcourir les occurrences
for($i=0;$i<sizeof($out[0]);$i++)
{
$longueurnombre = strlen((string)$out[0][$i][0]);
$taillechaine = strlen($chaine);
// découper la chaine en 3 morceaux
$debut = substr($chaine,0,$out[0][$i][1]);
$milieu = ($out[0][$i][0])-1;
$fin = substr($chaine,$out[0][$i][1]+$longueurnombre,$taillechaine);
// si c'est 10,100,1000 etc. on décale tout de 1 car le résultat comporte un chiffre
de moins
if(preg_match('#[1][0]+$#', $out[0][$i][0]))
{
for($j = $i+1;$j<sizeof($out[0]);$j++)
{
$out[0][$j][1] = $out[0][$j][1] -1;
}
}
$chaine = $debut.$milieu.$fin;
}
return $chaine;
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 80.15.50.242
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- https://master.php.net/note/edit/110147
Del: integrated -- https://master.php.net/note/delete/110147/integrated
Del: useless -- https://master.php.net/note/delete/110147/useless
Del: bad code -- https://master.php.net/note/delete/110147/bad+code
Del: spam -- https://master.php.net/note/delete/110147/spam
Del: non-english -- https://master.php.net/note/delete/110147/non-english
Del: in docs -- https://master.php.net/note/delete/110147/in+docs
Del: other reasons-- https://master.php.net/note/delete/110147
Reject -- https://master.php.net/note/reject/110147
Search -- https://master.php.net/manage/user-notes.php