I have a set of coordinates exported from google sketchup with extra fluff that I’ve been trying to strip with regex. I think it’s really interesting for quickly getting drawings in 3D from e.g. SketchUp into canvas from and .xsi file. The are multiple instances of data sets in one variable:
$str = 'SI_NurbsCurve Edge1 {
1,
0,
0,
4,
0,0,1,1,
2,
870.243,1229.35,143.395,1
927.537,1323.53,103.842,1
}
SI_NurbsCurve Edge2 {
1,
0,
0,
4,
0,0,1,1,
2,
899.54,1217.88,116.255,1
870.243,1229.35,143.395,1
}';
I’ve attempted to remove everything from the multiple instances except the coordinate data with this regex:
$reg = '#SI_NurbsCurve Edge[^"]* {
1,
0,
0,
4,
0,0,1,1,
2,#';
$rep="";
$str=preg_replace($reg,$rep,$str);
However, this result in only echoing the last coordinate set found in the string, in this example the following remains:
899.54,1217.88,116.255,1 870.243,1229.35,143.395,1
Besides that I’m trying to strip the last number “1” that occurs on each line of coordinates, so this entire example would end up looking like this:
870.243,1229.35,143.395, 927.537,1323.53,103.842, 899.54,1217.88,116.255, 870.243,1229.35,143.395,
I would be very grateful for your time and know-how!
Your first problem (getting only the last values) is probably caused by this:
You would need a non-greedy regex or if the value after
Edgeare just numbers:After that, you can chop of the last two characters of every remaining line.
You probably need to escape the
{character as well:\{and account for the}and spaces / new-lines after every set so the first line should be something like:See the working example (except for the last 2 chars of every line…) on Codepad.
To also get rid of the remaining
,1at the end of each line, you can change thepreg_replaceline with:This works on Codepad at least but probably depends on the encoding of the newlines.