note 110129 modified in function.preg-match-all by danbrown

From: Date: Thu, 06 Dec 2012 15:15:07 +0000
Subject: note 110129 modified in function.preg-match-all by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192361@lists.php.net to get a copy of this message
As I intended to create for my own purpose a clean PHP class to act on XML files, combining the use of DOM and simplexml functions, I had that small problem, but very annoying, that the offsets in a path is not numbered the same in both. That is to say, for example, if i get a DOM xpath object it appears like: /ANODE/ANOTHERNODE/SOMENODE[9]/NODE[2] and as a simplexml object would be equivalent to: ANODE->ANOTHERNODE->SOMENODE[8]->NODE[1] So u see what I mean? I used preg_match_all to solve that problem, and finally I got this after some hours of headlock (as I'm french the names of variables are in French sorry), hoping it could be useful to some of you: <?php function decrease_string($string) { /* retrieve all occurrences AND offsets of numbers in the original string: */ preg_match_all("/[0-9]+/",$chaine,$out,PREG_OFFSET_CAPTURE); for($i=0;$i<sizeof($out[0]);$i++) { $longueurnombre = strlen((string)$out[0][$i][0]); $taillechaine = strlen($chaine); // cut the string in 3 pieces $debut = substr($chaine,0,$out[0][$i][1]); $milieu = ($out[0][$i][0])-1; $fin = substr($chaine,$out[0][$i][1]+$longueurnombre,$taillechaine); /* if it's 10,100,1000, the problem is that the string gets shorter and it shifts all the offsets, so we have to decrease them of 1 */ 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; } ?> --was-- As I intended to create for my own purpose a clean PHP class to act on XML files, combining the use of DOM and simplexml functions, I had that small problem, but very annoying, that the offsets in a path is not numbered the same in both. That is to say, for example, if i get a DOM xpath object it appears like: /ANODE/ANOTHERNODE/SOMENODE[9]/NODE[2] and as a simplexml object would be equivalent to: ANODE->ANOTHERNODE->SOMENODE[8]->NODE[1] So u see what I mean? I used preg_match_all to solve that problem, and finally I got this after some hours of headlock (as I'm french the names of variables are in French sorry), hoping it could be useful to some of you: function decrease_string($string) { /* retrieve all occurrences AND offsets of numbers in the original string: */ preg_match_all("/[0-9]+/",$chaine,$out,PREG_OFFSET_CAPTURE); for($i=0;$i<sizeof($out[0]);$i++) { $longueurnombre = strlen((string)$out[0][$i][0]); $taillechaine = strlen($chaine); // cut the string in 3 pieces $debut = substr($chaine,0,$out[0][$i][1]); $milieu = ($out[0][$i][0])-1; $fin = substr($chaine,$out[0][$i][1]+$longueurnombre,$taillechaine); /* if it's 10,100,1000, the problem is that the string gets shorter and it shifts all the offsets, so we have to decrease them of 1 */ 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; } http://php.net/manual/en/function.preg-match-all.php

« previous php.notes (#192361) next »