note 110144 added to function.preg-match
| From: | Anonymous | Date: | Mon, 24 Sep 2012 08:02:08 +0000 |
| Subject: | note 110144 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190985@lists.php.net to get a copy of this message | ||
Here is a function that decreases the numbers inside a string (useful to convert DOM object into
simplexml object)
e.g.: decremente_chaine("somenode->anode[2]->achildnode[3]") will return
"somenode->anode[1]->achildnode[2]"
the numbering of the nodes in simplexml starts from zero, but from 1 in DOM xpath objects
<?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.php
Edit -- https://master.php.net/note/edit/110144
Del: integrated -- https://master.php.net/note/delete/110144/integrated
Del: useless -- https://master.php.net/note/delete/110144/useless
Del: bad code -- https://master.php.net/note/delete/110144/bad+code
Del: spam -- https://master.php.net/note/delete/110144/spam
Del: non-english -- https://master.php.net/note/delete/110144/non-english
Del: in docs -- https://master.php.net/note/delete/110144/in+docs
Del: other reasons-- https://master.php.net/note/delete/110144
Reject -- https://master.php.net/note/reject/110144
Search -- https://master.php.net/manage/user-notes.php