note 110129 added to function.preg-match-all

From: Date: Sat, 22 Sep 2012 17:52:20 +0000
Subject: note 110129 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-190960@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: 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; } ---- Server IP: 69.147.83.199 Probable Submitter: 82.229.255.161 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- https://master.php.net/note/edit/110129 Del: integrated -- https://master.php.net/note/delete/110129/integrated Del: useless -- https://master.php.net/note/delete/110129/useless Del: bad code -- https://master.php.net/note/delete/110129/bad+code Del: spam -- https://master.php.net/note/delete/110129/spam Del: non-english -- https://master.php.net/note/delete/110129/non-english Del: in docs -- https://master.php.net/note/delete/110129/in+docs Del: other reasons-- https://master.php.net/note/delete/110129 Reject -- https://master.php.net/note/reject/110129 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190960) next »