0

I have tried to wrap my head around regex and have failed pretty miserably. Here is my problem.

how would you do the following with regular expressions in php.

replace

<use xlink:href='#gpPt4' transform='translate([B][U]261.7,184.3[/U][/B]) scale([B][U]4.50[/U][/B])'/>

with

<use xlink:href='#gpPt4' transform='translate([B][U]261.7,184.3[/U][/B]) scale([B][U]4.50[/U][/B])' onclick="alert('a variable name from php')"/>

where the bolded/underlined sections (only used for emphasis, not actually in the text) are variable and change from line to line
ie i need to run this on a file with multiple lines similar to above with different numbers and be able to insert the alert with a variable sent in from php

to add slightly more information, the above lines are from an SVG (an XML based file type used for drawing infinitely scalable images) file. I am generating the SVG file using gnuplot (i think) and the output is many lines like the first line of code above. I was planning on reading in the file using PHP, changing it with regex, and writing it back out. If there is a better way of doing this please let me know.

Edited by j_p36: added last paragraph to better explain why I'm having problem

3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by j_p36
0
<?php
$pattern = "{\/\>}";
$replace = " onclick=\"alert('a variable name from php')\"/>";
$string = "<use xlink:href='#gpPt4' transform='translate(261.7,184.3) scale(4.50)'/>";

$newstring = preg_replace($pattern, $replace, $string);
echo $newstring;
?>

Edited by genevish: n/a

0
<?php
$pattern = "{\/\>}";
$replace = " onclick=\"alert('a variable name from php')\"/>";
$string = "<use xlink:href='#gpPt4' transform='translate(261.7,184.3) scale(4.50)'/>";

$newstring = preg_replace($pattern, $replace, $string);
echo $newstring;
?>

Wont this code just go in and put onclick=\"alert('a variable name from php') after every "/>"?

I should be more specific. Below is a few snippets from an example SVG file I am creating. I need to make sure that only the "...transform='translate..." lines are changed. Sorry I wasn't more clear about that originally. I do really appreciate any help though.

(the dots are where I left out code to ease reading and just show some of the types of information I need to not touch when parsing because the original file is long)

.
.
.
<use xlink:href='#gpPt9' id='gpPt10' fill='currentColor' stroke='none'/>
	<use xlink:href='#gpPt3' id='gpPt11' stroke='currentColor' transform='rotate(45)'/>
	<use xlink:href='#gpPt11' id='gpPt12' fill='currentColor' stroke='none'/>
</defs>
<g style="fill:none; color:white; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M37.3,444.0 L575.0,444.0 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M37.3,444.0 L46.3,444.0 M575.0,444.0 L566.0,444.0 '></path>
	<g transform="translate(29.0,448.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>-3</text>
	</g>
</g>
.
.
.
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M575.0,444.0 L575.0,435.0 M575.0,16.7 L575.0,25.7 '></path>
	<g transform="translate(575.0,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text> 20</text>
	</g>
	<path  d='M37.3,16.7 L37.3,444.0 L575.0,444.0 L575.0,16.7 L37.3,16.7 Z '></path>
	<a xlink:href="../display_virus.php?v=HK/1/68">
	<g transform="translate(86.2,144.2)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.HK/1/68</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/93/70">
	<g transform="translate(408.5,300.9)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.BI/93/70</text>
	</g>
	</a>
.
.
.
<a xlink:title="Plot #1" xlink:show="new">
<g style="fill:none; color:red; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<g transform="translate(507.9,430.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>Viruses</text>
	</g>
	<use xlink:href='#gpPt4' transform='translate(343.4,220.7) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(535.3,73.6) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(394.6,112.0) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(86.2,139.7) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(392.8,402.0) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(356.7,317.7) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(400.4,323.3) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(408.5,296.4) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(367.3,349.3) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(314.1,373.9) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(537.3,426.0) scale(4.50)'/>
</g>
	</a>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M37.3,16.7 L37.3,444.0 L575.0,444.0 L575.0,16.7 L37.3,16.7 Z '></path>
</g>
</svg>

Edited by j_p36: n/a

0

Perhaps this?

$alertVar = 'Your variable here';
$pattern = "{transform='translate\((.+?)\) scale\((.+?)\)'\/\>}";
$replace = "transform='translate($1) scale($2)' onclick=\"alert('$alertVar')\"/>";
$string = '<a xlink:title="Plot #1" xlink:show="new"/>
<g style="fill:none; color:red; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<g transform="translate(507.9,430.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>Viruses</text>
	</g>
	<use xlink:href=\'#gpPt4\' transform=\'translate(343.4,220.7) scale(4.50)\'/>
	<use xlink:href=\'#gpPt4\' transform=\'translate(535.3,73.6) scale(4.50)\'/>
	<use xlink:href=\'#gpPt4\' transform=\'translate(394.6,112.0) scale(4.50)\'/>
	';
	

$newstring = preg_replace($pattern, $replace, $string);
0

thanks ezzaral, this looks promising, I'm going to try this either later tonight or tomorrow and get back to you

0

Ok having some serious problems.
I'm using the below code.

$svg = $resFileName;
                        if(file_exists($svg))
                        {
                            $file1=fopen($uploadfile,'r');
                            while(!feof($file1))
                            {
                                $theData=fgets($file1);
                                $temparray[]=preg_split("/[:\s,]+/",$theData, -1, PREG_SPLIT_NO_EMPTY);
                            }
                            fclose($file1);
                            for($x=2; $x<count($temparray); $x++)
                            {
                                $labels[]=$temparray[$x][0];
                            }
//above code is pulling information from another file to populate the alert function with
                            $labelCount = 0;
                            $file=fopen($svg,'r');
                            while(!feof($file))
                            {
                                $theData=fgets($file);
                                $pattern = "{transform='translate\((.+?)\) scale\((.+?)\)'\/\>}";
                                if(preg_match($pattern, $theData)>0)
                                {
                                    $alertVar = $labels[$labelCount++];
                                    $replace = "transform='translate($1) scale($2)' onclick=\"alert('$alertVar')\"/>";
                                    $changedData = preg_replace($pattern, $replace, $theData);
                                    $temparray1[]=$changedData;
                                }
                                else
                                {
                                    $temparray1[]=$theData;
                                }
                            }
                            fclose($file);
                            print_r($temparray1);
//                            $file2=fopen($svg, 'w');
//                            for($x=0; $x<count($temparray1); $x++)
//                            {
//                                fwrite($file2, $temparray1[x]."/n");
//                            }
//                            fclose($file2);
// this code is for writing everything back out
                        }

and I get this if I do a print_r($temparray1)

Array ( [0] => [1] => "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> [3] => xmlns="http://www.w3.org/2000/svg" [5] => xmlns:xlink="http://www.w3.org/1999/xlink" [6] => > [7] => [8] => Produced by GNUPLOT 4.5 patchlevel 0 [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => -2.5 [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => -2 [47] => [48] => [49] => [50] => [51] => [52] => [53] => [54] => [55] => -1.5 [56] => [57] => [58] => [59] => [60] => [61] => [62] => [63] => [64] => -1 [65] => [66] => [67] => [68] => [69] => [70] => [71] => [72] => [73] => -0.5 [74] => [75] => [76] => [77] => [78] => [79] => [80] => [81] => [82] => 0 [83] => [84] => [85] => [86] => [87] => [88] => [89] => [90] => [91] => 0.5 [92] => [93] => [94] => [95] => [96] => [97] => [98] => [99] => [100] => 1 [101] => [102] => [103] => [104] => [105] => [106] => [107] => [108] => [109] => 1.5 [110] => [111] => [112] => [113] => [114] => [115] => [116] => [117] => [118] => 2 [119] => [120] => [121] => [122] => [123] => [124] => [125] => [126] => [127] => 2.5 [128] => [129] => [130] => [131] => [132] => [133] => [134] => [135] => [136] => 3 [137] => [138] => [139] => [140] => [141] => [142] => [143] => [144] => [145] => -25 [146] => [147] => [148] => [149] => [150] => [151] => [152] => [153] => [154] => -20 [155] => [156] => [157] => [158] => [159] => [160] => [161] => [162] => [163] => -15 [164] => [165] => [166] => [167] => [168] => [169] => [170] => [171] => [172] => -10 [173] => [174] => [175] => [176] => [177] => [178] => [179] => [180] => [181] => -5 [182] => [183] => [184] => [185] => [186] => [187] => [188] => [189] => [190] => 0 [191] => [192] => [193] => [194] => [195] => [196] => [197] => [198] => [199] => 5 [200] => [201] => [202] => [203] => [204] => [205] => [206] => [207] => [208] => 10 [209] => [210] => [211] => [212] => [213] => [214] => [215] => [216] => [217] => 15 [218] => [219] => [220] => [221] => [222] => [223] => [224] => [225] => [226] => 20 [227] => [228] => [229] => [230] => [231] => .BI/16398/68 [232] => [233] => [234] => [235] => [236] => . [237] => [238] => [239] => [240] => [241] => . [242] => [243] => [244] => [245] => [246] => . [247] => [248] => [249] => [250] => [251] => . [252] => [253] => [254] => [255] => [256] => . [257] => [258] => [259] => [260] => [261] => . [262] => [263] => [264] => [265] => [266] => . [267] => [268] => [269] => [270] => [271] => . [272] => [273] => [274] => [275] => [276] => . [277] => [278] => [279] => [280] => [281] => [282] => [283] => Viruses [284] => [285] => [286] => [287] => [288] => [289] => [290] => [291] => [292] => [293] => [294] => [295] => [296] => [297] => [298] => [299] => [300] => [301] => [302] => [303] => )

why is the array practically empty? does it have something to do with an SVG file being XML?

if I uncomment the last view lines of the code above the svg turns into nothing but a lot of "\n"'s

here is the original svg file before my code edits it

<?xml version="1.0" encoding="utf-8"  standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="600" height="480" viewBox="0 0 600 480"
 xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink"
>

<desc>Produced by GNUPLOT 4.5 patchlevel 0 </desc>

<defs>

	<circle id='gpDot' r='0.5' stroke-width='0.5'/>
	<path id='gpPt0' stroke-width='0.222' stroke='currentColor' d='M-1,0 h2 M0,-1 v2'/>
	<path id='gpPt1' stroke-width='0.222' stroke='currentColor' d='M-1,-1 L1,1 M1,-1 L-1,1'/>
	<path id='gpPt2' stroke-width='0.222' stroke='currentColor' d='M-1,0 L1,0 M0,-1 L0,1 M-1,-1 L1,1 M-1,1 L1,-1'/>
	<rect id='gpPt3' stroke-width='0.222' stroke='currentColor' x='-1' y='-1' width='2' height='2'/>
	<rect id='gpPt4' stroke-width='0.222' stroke='currentColor' fill='currentColor' x='-1' y='-1' width='2' height='2'/>
	<circle id='gpPt5' stroke-width='0.222' stroke='currentColor' cx='0' cy='0' r='1'/>
	<use xlink:href='#gpPt5' id='gpPt6' fill='currentColor' stroke='none'/>
	<path id='gpPt7' stroke-width='0.222' stroke='currentColor' d='M0,-1.33 L-1.33,0.67 L1.33,0.67 z'/>
	<use xlink:href='#gpPt7' id='gpPt8' fill='currentColor' stroke='none'/>
	<use xlink:href='#gpPt7' id='gpPt9' stroke='currentColor' transform='rotate(180)'/>
	<use xlink:href='#gpPt9' id='gpPt10' fill='currentColor' stroke='none'/>
	<use xlink:href='#gpPt3' id='gpPt11' stroke='currentColor' transform='rotate(45)'/>
	<use xlink:href='#gpPt11' id='gpPt12' fill='currentColor' stroke='none'/>
</defs>
<g style="fill:none; color:white; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,444.0 L575.0,444.0 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,444.0 L62.9,444.0 M575.0,444.0 L566.0,444.0 '></path>
	<g transform="translate(45.6,448.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>-2.5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,405.2 L575.0,405.2 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,405.2 L62.9,405.2 M575.0,405.2 L566.0,405.2 '></path>
	<g transform="translate(45.6,409.7)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>-2</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,366.3 L575.0,366.3 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,366.3 L62.9,366.3 M575.0,366.3 L566.0,366.3 '></path>
	<g transform="translate(45.6,370.8)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>-1.5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,327.5 L575.0,327.5 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,327.5 L62.9,327.5 M575.0,327.5 L566.0,327.5 '></path>
	<g transform="translate(45.6,332.0)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>-1</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,288.6 L575.0,288.6 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,288.6 L62.9,288.6 M575.0,288.6 L566.0,288.6 '></path>
	<g transform="translate(45.6,293.1)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>-0.5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,249.8 L575.0,249.8 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,249.8 L62.9,249.8 M575.0,249.8 L566.0,249.8 '></path>
	<g transform="translate(45.6,254.3)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 0</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,210.9 L575.0,210.9 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,210.9 L62.9,210.9 M575.0,210.9 L566.0,210.9 '></path>
	<g transform="translate(45.6,215.4)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 0.5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,172.1 L575.0,172.1 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,172.1 L62.9,172.1 M575.0,172.1 L566.0,172.1 '></path>
	<g transform="translate(45.6,176.6)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 1</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,133.2 L575.0,133.2 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,133.2 L62.9,133.2 M575.0,133.2 L566.0,133.2 '></path>
	<g transform="translate(45.6,137.7)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 1.5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,94.4 L575.0,94.4 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,94.4 L62.9,94.4 M575.0,94.4 L566.0,94.4 '></path>
	<g transform="translate(45.6,98.9)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 2</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,55.5 L575.0,55.5 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,55.5 L62.9,55.5 M575.0,55.5 L566.0,55.5 '></path>
	<g transform="translate(45.6,60.0)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 2.5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,16.7 L575.0,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,16.7 L62.9,16.7 M575.0,16.7 L566.0,16.7 '></path>
	<g transform="translate(45.6,21.2)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text> 3</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,444.0 L53.9,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,444.0 L53.9,435.0 M53.9,16.7 L53.9,25.7 '></path>
	<g transform="translate(53.9,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text>-25</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M111.8,444.0 L111.8,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M111.8,444.0 L111.8,435.0 M111.8,16.7 L111.8,25.7 '></path>
	<g transform="translate(111.8,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text>-20</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M169.7,444.0 L169.7,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M169.7,444.0 L169.7,435.0 M169.7,16.7 L169.7,25.7 '></path>
	<g transform="translate(169.7,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text>-15</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M227.6,444.0 L227.6,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M227.6,444.0 L227.6,435.0 M227.6,16.7 L227.6,25.7 '></path>
	<g transform="translate(227.6,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text>-10</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M285.5,444.0 L285.5,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M285.5,444.0 L285.5,435.0 M285.5,16.7 L285.5,25.7 '></path>
	<g transform="translate(285.5,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text>-5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M343.4,444.0 L343.4,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M343.4,444.0 L343.4,435.0 M343.4,16.7 L343.4,25.7 '></path>
	<g transform="translate(343.4,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text> 0</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M401.3,444.0 L401.3,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M401.3,444.0 L401.3,435.0 M401.3,16.7 L401.3,25.7 '></path>
	<g transform="translate(401.3,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text> 5</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M459.2,444.0 L459.2,435.0 M459.2,417.0 L459.2,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M459.2,444.0 L459.2,435.0 M459.2,16.7 L459.2,25.7 '></path>
	<g transform="translate(459.2,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text> 10</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M517.1,444.0 L517.1,435.0 M517.1,417.0 L517.1,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M517.1,444.0 L517.1,435.0 M517.1,16.7 L517.1,25.7 '></path>
	<g transform="translate(517.1,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text> 15</text>
	</g>
</g>
<g style="fill:none; color:gray; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M575.0,444.0 L575.0,16.7 '></path>
</g>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M575.0,444.0 L575.0,435.0 M575.0,16.7 L575.0,25.7 '></path>
	<g transform="translate(575.0,466.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:middle">
		<text> 20</text>
	</g>
	<path  d='M53.9,16.7 L53.9,444.0 L575.0,444.0 L575.0,16.7 L53.9,16.7 Z '></path>
	<a xlink:href="../display_virus.php?v=BI/16398/68">
	<g transform="translate(348.0,49.0)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.BI/16398/68</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/15793/68">
	<g transform="translate(367.2,280.9)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/16190/68">
	<g transform="translate(81.6,102.3)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=HK/1/68">
	<g transform="translate(551.0,40.9)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/808/69">
	<g transform="translate(343.2,328.8)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/908/69">
	<g transform="translate(357.2,300.4)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/17938/69">
	<g transform="translate(331.9,336.4)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/93/70">
	<g transform="translate(332.4,298.2)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/2668/70">
	<g transform="translate(344.9,387.0)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
	<a xlink:href="../display_virus.php?v=BI/6449/71">
	<g transform="translate(376.5,418.6)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:start">
		<text>.</text>
	</g>
	</a>
</g>
	<a xlink:title="Plot #1" xlink:show="new">
<g style="fill:none; color:red; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<g transform="translate(507.9,430.5)" style="stroke:none; fill:black; font-family:Arial; font-size:12.00pt; text-anchor:end">
		<text>Viruses</text>
	</g>
	<use xlink:href='#gpPt4' transform='translate(367.2,276.4) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(81.6,97.8) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(348.0,44.5) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(551.0,36.4) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(343.2,324.3) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(357.2,295.9) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(331.9,331.9) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(332.4,293.7) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(344.9,382.5) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(376.5,414.1) scale(4.50)'/>
	<use xlink:href='#gpPt4' transform='translate(537.3,426.0) scale(4.50)'/>
</g>
	</a>
<g style="fill:none; color:black; stroke:currentColor; stroke-width:1.00; stroke-linecap:butt; stroke-linejoin:miter">
	<path  d='M53.9,16.7 L53.9,444.0 L575.0,444.0 L575.0,16.7 L53.9,16.7 Z '></path>
</g>
</svg>
0

I modified your code just a little (see below) to use a static alert() message (I didn't have a file for those) and fixed the minor error with the 'x' variable in your fwrite() code, and it did the replacement just fine. If you're looking at your print_r($temparray1) output in a browser, you won't see most of the text because it's markup with very little textual content. Look at the page source and you'll see all of the tags as text.

<?php
$svg = 'testIn.xml';
                        if(file_exists($svg))
                        {
                            // $file1=fopen($uploadfile,'r');
                            // while(!feof($file1))
                            // {
                                // $theData=fgets($file1);
                                // $temparray[]=preg_split("/[:\s,]+/",$theData, -1, PREG_SPLIT_NO_EMPTY);
                            // }
                            // fclose($file1);
                            // for($x=2; $x<count($temparray); $x++)
                            // {
                                // $labels[]=$temparray[$x][0];
                            // }
//above code is pulling information from another file to populate the alert function with
                            $labelCount = 0;
                            $file=fopen($svg,'r');
                            while(!feof($file))
                            {
                                $theData=fgets($file);
                                $pattern = "{transform='translate\((.+?)\) scale\((.+?)\)'\/\>}";
                                if(preg_match($pattern, $theData)>0)
                                {
				    echo "Match<BR>";
                                    $alertVar = 'hi';
                                    $replace = "transform='translate($1) scale($2)' onclick=\"alert('$alertVar')\"/>";
                                    $changedData = preg_replace($pattern, $replace, $theData);
                                    $temparray1[]=$changedData;
                                }
                                else
                                {
                                    $temparray1[]=$theData;
                                }
                            }
                            fclose($file);
                            print_r($temparray1);
							
                           $file2=fopen('testOut.xml', 'w');
                           for($x=0; $x<count($temparray1); $x++)
                           {
                               fwrite($file2, $temparray1[$x]);
                           }
                           fclose($file2);
//this code is for writing everything back out
                        }

?>
0

Wow, I didn't even think about the webpage parsing everything and needing to view page source. Sorry... I will check this out either some time this weekend or early next week. I really appreciate the help.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.