How to diff file's apostrophe text in PHP - php

I want to diff 2 files and ignore checking lines that doesn't have apostrophe ("text") in PHP
For example:
File 1:
START
LTEXT "Tool Version:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
File 2:
/*
* Translated by Saibamen
*
/
START
LTEXT "Wersja narzędzia:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
I want to check if there's any difference between Tool Version: and Wersja narzędzia: strings.
Note: Files doesn't have this same schemas line by line - I want to start diff lines from line that have START in file.

You should first get content of each file and then use preg_match function with proper pattern to find version line and version string.
$fileContent1 = <<<TXT
dummy
dummy
LTEXT "Tool Version:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
dummy
TXT;
$fileContent2 = <<<TXT
dummy
LTEXT "Wersja narzędzia:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT
dummy
dummy
TXT;
function diff_versions($leftContent, $rightContent) {
$diff = true;
$leftVersion = null;
$rightVersion = null;
$pattern = '/LTEXT "(Tool Version|Wersja narzędzia):", (.*)\R?/';
if (preg_match($pattern, $leftContent, $matches) !== 1) {
throw new Exception('Left content has no version line.');
}
$leftVersion = $matches[2];
if (preg_match($pattern, $rightContent, $matches) !== 1) {
throw new Exception('Right content has no version line.');
}
$rightVersion = $matches[2];
return array(
'diff' => $leftVersion === $rightVersion,
'leftVersion' => $leftVersion,
'rightVersion' => $rightVersion,
);
}
var_dump(diff_versions($fileContent1, $fileContent2));
Output:
array (size=3)
'diff' => boolean true
'leftVersion' => string 'IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT' (length=89)
'rightVersion' => string 'IDC_STATIC, 70, 150, 80, 10, SS_RIGHT EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT' (length=89)
And then you should diff versions as you like.

Maybe this will give a point to start with:
$str1 = 'LTEXT "Tool Version:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT';
$str2 = 'LTEXT "Wersja narzędzia:", IDC_STATIC, 70, 150, 80, 10, SS_RIGHT
EDITTEXT IDC_STATIC_TIME, 155, 50, 210, 10, ES_LEFT';
$shortStr1 = substr($str1, 23);
$shortStr2 = substr($str2, 28);
echo "\n";
echo substr_compare($shortStr1, $shortStr2, 0, strlen($shortStr1));
echo "\n";
echo substr_compare($shortStr2, $shortStr1, 0, strlen($shortStr2));
Two compares if the Strings are not the same size
Will return 0 if same
Will return 1 if not same
For a better test you have to test via preg_match() and pre_match_all()
I think this - or at least the idea - will lead you to the answer.
Next time provide more Information and what you already have tried and many people here will even provide full solutions!
But for that you have to show what you already got and already tried. No one wants to do the work you are paid for, but many here are willing to help you, if you have any problem and tried hard enough!

Related

Output to file doesn't look the way it should

So I try to send some output to a file but it doesn't look the way I want.
I have a file with some random integer values , it looks like this:
78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73
4, 6, 9, 11, 16
9, 10, 16, 18, 20
1, 3, 8, 10, 15
Now what I want to do is for each of these lines print the average, max and min number into another file.
My code for this part is:
while (!feof($aFile))
{
$cnt += 1;
$anArray = array();
$anArray = explode(",", fgets($aFile));
foreach ($anArray as $value)
{
$value = (int)$value;
}
$line = "Line $cnt: Average=" . array_sum($anArray) / count($anArray) . " Max=" . max($anArray) . " Min=" . min($anArray);
fwrite($resultsFile, $line);
fwrite($resultsFile, "\n");
}
The problem is that the resultsFile looks like this:
Line 1: Average=70.6 Max= 85 Min= 60
Line 2: Average=9.2 Max= 16
Min=4
Line 3: Average=14.6 Max= 20
Min=9
Line 4: Average=7.4 Max= 15 Min=1
I couldn't exactly copy paste this , because when I do it pastes it the right way
The problem is that it changes line and then prints Min .
Total Average=50.533333333333 Total Max= 85 Total Min=1
This is kinda tricky and is a combination of several moments.
First - you have new line symbol (\n) in each string of your file.
So, for example string 9, 10, 16, 18, 20 is really a 9, 10, 16, 18, 20\n string.
When you explode this string by ,, last element of your $anArray is not 20, but 20\n (with new line).
Now you're trying to cast each element to int, but as by default php works with copy of array, your initial values in $anArray are unchaged. As correctly stated in comments - to apply changes to original array, use $&value notation:
foreach ($anArray as &$value)
{
$value = (int)$value;
}
And last - all following operations on your $anArray silenlty cast 20\n to int (20). But as noticed in max manual, for example:
The actual value returned will be of the original type with no conversion applied
So, in array ['9', '10', '16', '18', '20\n'] max value will be 20, but returned value will be 20\n which creates a new line and moves Min=... output to next line.

FPDF Put Header + Styling on Auto Page Break Pages

I'm using $pdf->SetAutoPageBreak(true); to auto break long outputs over multiple pages but I have some styling that I would like to apply to the auto-created pages:
// HEADER ------------------------------------------------------------------
$pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] );
$pdf->SetFont( 'Arial', '', 17 );
$pdf->Cell( 0, 15, $reportName, 0, 0, 'C' );
$pdf->Ln( 16 );
$pdf->SetLineWidth(7);
$pdf->SetDrawColor($col_r, $col_g, $col_b);
$pdf->Line(0, 0, 0, 300);
$pdf->SetLineWidth(0.5);
$pdf->Line(20, 26, 210-20, 26);
$pdf->SetTextColor( 0,0,0 );
$pdf->SetFont( 'Arial', 'B', 24 );
$pdf->Cell( 0, 15, 'Results', 0, 0, 'C' );
$pdf->Line(20, 40, 210-20, 40);
$pdf->Ln( 16 );
$pdf->Write( 6, $file ); // This is about 3 pages long worth of text
This adds a header-style block to the top of the page as well as a border on the left hand side all the way down the page. How can I replicate this on each page when the data is too big?
You can create extend class to automatically create the header.
Please check the example at http://www.fpdf.org/en/tutorial/tuto2.htm OR FPDF Tutorial for more information for the others sample.

TCPDF draw circle with border and fill

I'm using TCPDF in my php application, but I'm not finding a way to draw a circle with black border and filled with another color;
here my code:
$style_bollino = array('width' => 0.25, 'dash' => 0, 'color' => array(0, 0, 0));
$this->SetAlpha(1);
$this->Circle(35, 100, 4, 0, 360, 'C', $style_bollino, array(210, 0, 0));
I tried also to change 'C' parameter to 'F' or null but I didn't get the result.
I'm not able to figure out what I'm missing
kind regards,
Matt
According to one of their examples, there's a one-liner way to do it, just pass 'DF' to $style parameter:
$this->Circle(35, 100, 4, 0, 360, 'DF', $style_bollino, array(210, 0, 0));
For a list of options for this parameter, check the PHPDoc in TCPDF_STATIC::getPathPaintOperator() function.
This seems like a fairly straightforward requirement but I can't work out how to do it in one line either.
However, it's pretty simple to fill the circle and then draw another stroked circle on top of it.
foreach (array("F", "S") as $fill_style) {
$this->Circle(35, 100, 4, 0, 360, $fill_style, $style_bollino, array(210, 0, 0));
}

Sending multidimensional array from jQuery to php

I am passing an array from jQuery to php.
The array is generated from a table with this code:
var stitchChartArray = [];
var row = 0;
// turn stitch chart into array for php
$('#stitchChart').find('tr').each(function (index, obj) {
//first row is table head- "Block #"
if(index != 0){
stitchChartArray.push([]);
var TDs = $(this).children();
$.each(TDs, function (i, o) {
var cellData = [$(this).css('background-color'), $(this).find("img").attr('src')];
stitchChartArray[row].push(cellData);
});
row++;
}
});
In console it looks like this:
[[["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], 7 more...], [["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(98, 119, 57)", "symbols/210.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(150, 150, 195)", "symbols/72.png"], 7 more...], [["rgb(75, 165, 105)", "symbols/187.png"], ["rgb(134, 158, 134)", "symbols/64.png"], ["rgb(165, 180, 180)", "symbols/171.png"], 7 more...], [["rgb(60, 150, 75)", "symbols/189.png"], ["rgb(120, 120, 90)", "symbols/225.png"], ["rgb(143, 163, 89)", "symbols/209.png"], 7 more...]]
It represents each row of a table->each cell of row->[0]rgb value of bg of cell [1]icon in cell.
This jQuery code returns the correct element(and rgb value) from the array:
alert(stitchChartArray[1][1][0]); //row 1,cell 1, first value(rgb)
But when it gets sent to the php script with this:
$.post('makeChartPackage.php', {'stitchChart[]': stitchChartArray }, function(data){
alert(data);
});
The php throws an error:
Cannot use string offset as an array in /Users/tnt/Sites/cross_stitch/makeChartPackage.php on line 33
$stitchChart = $_POST['stitchChart'];
echo $stitchChart[1][1][0]; //line 33
I am assuming I am either constructing the array incorrectly or passing it to the php script incorrectly.
EDIT:
I did this to return the array to jQuery:
$stitchChart = $_POST['stitchChart'];
print_r($stitchChart);
And here was the result:
Array
(
[0] => rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(180, 195, 105),symbols/388.png,rgb(165, 165, 120),symbols/235.png,rgb(75, 75, 60),symbols/184.png,rgb(90, 90, 45),symbols/195.png,rgb(120, 120, 75),symbols/156.png,rgb(105, 105, 105),symbols/163.png
[1] => rgb(105, 105, 105),symbols/163.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(165, 165, 120),symbols/235.png,rgb(120, 120, 75),symbols/156.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png
[2] => rgb(105, 105, 105),symbols/163.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png,rgb(105, 105, 105),symbols/163.png
It appears the array is not multidimensional?
$_POST['stitchChart'] in the context you have addressed it there is (effectively) a JSON representation of a multidimensional array, stored as a string. When you treat a string as a multidimensional indexed array in PHP, you will get that error. The first [x] is treated as a "string offset" - i.e. the character at position x - but the next and any subsequent [x] addresses can only be treated as arrays (you cannot get a substring of a single character) and will emit the error you have received.
To access your data as an array in PHP, you need to use json_decode():
$stitchChart = json_decode($_POST['stitchChart'],TRUE);
echo $stitchChart[1][1][0];
EDIT
Because the jQuery data argument seemingly can't deal with multidimensional arrays, you should use Douglas Crockford's JSON-js library and pass the result into data as a string. NB: use json2.js.
Here is how you could do this:
stitchChartArray = JSON.stringify(stitchChartArray);
$.post('makeChartPackage.php', {'stitchChart': stitchChartArray }, function(data){
alert(data);
});
If you use this code, my original PHP suggestion should work as expected.

Google Goggles API workaround issues in PHP

I've been tearing my hair out with the following all day:
I'm attempting to send manual requests to Google Goggles with PHP-ported code from this blog post: http://notanothercodeblog.blogspot.com/2011/02/google-goggles-api.html
The original blog post has apparantly working code written in .NET, but my platform is PHP. The code I have is:
$googleID = substr(md5(rand()), 0, 16);
/* */
$url = 'http://www.google.com/goggles/container_proto?cssid=' . $googleID;
// $url = 'http://www.google.com/goggles/container_proto?cssid=';
// $googleCodes = array(0x22,0x00,0x62,0x3C,0x0A,0x13,0x22,0x02,0x65,0x6E,0xBA,0xD3,0xF0,0x3B,0x0A,0x08,0x01,0x10,0x01,0x28,0x01,0x30,0x00,0x38,0x01,0x12,0x1D,0x0A,0x09,0x69,0x50,0x68,0x6F,0x6E,0x65,0x20,0x4F,0x53,0x12,0x03,0x34,0x2E,0x31,0x1A,0x00,0x22,0x09,0x69,0x50,0x68,0x6F,0x6E,0x65,0x33,0x47,0x53,0x1A,0x02,0x08,0x02,0x22,0x02,0x08,0x01);
$googleCodes = array(34,0,98,60,10,19,34,2,101,110,186,211,240,59,10,8,1,16,1,40,1,48,0,56,1,18,29,10,9,105,80,104,111,110,101,32,79,83,18,3,52,46,49,26,0,34,9,105,80,104,111,110,101,51,71,83,26,2,8,2,34,2,8,1);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, array("CssidPostBody" => implode("",$googleCodes)));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/x-protobuffer",
"Pragma: no-cache"));
$response = curl_exec($handle);
/* */
print_r($response);
I keep getting the following response back from Google:
"Unparseable Container Request"
I believe my issue is with the format of $googleCodes. The original article has the following for it:
private static byte[] CssidPostBody = new byte[] { 34, 0, 98, 60, 10, 19, 34,
2, 101, 110, 186, 211, 240, 59, 10, 8, 1, 16, 1, 40, 1, 48, 0, 56, 1, 18,
29, 10, 9, 105, 80, 104, 111, 110, 101, 32, 79, 83, 18, 3, 52, 46, 49,
26, 0, 34, 9, 105, 80, 104, 111, 110, 101, 51, 71, 83, 26, 2, 8, 2, 34,
2, 8, 1 };
I realise this is an issue that is potentially very unique, but I'm hoping someone with experience with .NET byte arrays could point me in the right direction.
$googleCodes = array(34,0,98,60,10,19,34,2,101,110,186,211,240,59,10,8,1,16,1,40,1,48,0,56,1,18,29,10,9,105,80,104,111,110,101,32,79,83,18,3,52,46,49,26,0,34,9,105,80,104,111,110,101,51,71,83,26,2,8,2,34,2,8,1);
foreach ($googleCodes as $n=>$char) { $googleCodes[$n] = chr($char); }
I think these are codes for the characters, and I suspect they're being sent in binary. Either that, or they should be zero padded. Can you provide a link to the original method in the foreign language? We just want to emulate their method. I think your numbers are running together, so it's a question of the desired format.

Categories