Undesirable rounding off of numbers in generated PHPexcel - mysql

I am generating a downloadable excel file from mysql database using phpexcel. In that, there is a field having 18 digit number, which in the mysql, it is defined as Bigint. The number is defined as a hyperlink in the code. Now, there is the following problem -
The last 4 digits of the number hyperlink are displayed as 0000 although on clicking the number hyperlink, it is opening correctly. Example 860814069447613475 is shown as 860814069447610000 in the generated excel
Here is my code -
$objPHPExcel->getActiveSheet()
->getStyle('A'.(string)$n)
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_NUMBER
);
$n = 2;
while ($row = mysqli_fetch_array($result)){
$objPHPExcel->getActiveSheet()->setCellValue('A'.(string)$n,
$row['t_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.(string)$n,
$row['t_text']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.(string)$n,
$row['user_name']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.(string)$n,
$row['description']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.(string)$n
,$row['time']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.(string)$n,
$row['place']);
$objPHPExcel->getActiveSheet()->getStyle("A$n:F$n")->getAlignment()-
>setWrapText(true);
$objPHPExcel->getActiveSheet()
->getCell('A'.(string)$n)
->getHyperlink()
->setUrl('http://t.com/'.$row['user_name'].'/status/' . $row['t_id']);
// Config
$link_style_array = [
'font' => [
'color' => ['rgb' => '0000FF'],
'underline' => 'single'
]
];
// Set it!
$objPHPExcel->getActiveSheet()->getStyle('A'.(string)$n)->applyFromArray($link_style_array);
$n++;
}

Found code which worked for me -
setCellValueExplicit('A'.(string)$n, $row['t_id'],
PHPExcel_Cell_DataType::TYPE_STRING);

Related

using PHP simple html dom get attributes name in span?

I am not if 'tags' are the right term but i have to get the "data-time" values from this span into an array. How can I use simple html dom to get them?
Here is on span I am trying to get the "data-time" out of.
include('../simpleHtmlDom/simple_html_dom.php');
// Put the Twitters username here
$user = "yadayada";
$html = file_get_html("https://twitter.com/$user");
$ret = $html->find('div[class=ProfileTweet-contents]');
$ret = $html->find('p[class=ProfileTweet-text js-tweet-text u-dir]');
/// tries to get the time code but does only gets the span
$date = $html->find('span[class=js-short-timestamp js-relative-timestamp]', 0);
$DoesNotWork = $html->find( "data-time", 0 );
echo $ret[1]; // get's a users tweet.
echo $DoesNotWork;
The result of the date
<span class="js-short-timestamp js-relative-timestamp"
data-time="1401528672"
data-long-form="true">
15h
</span>
I would think it is something like this but this code does not work.
$html->find( "data-time", 0 );
You may try this:
// Include the script
$url = 'https://twitter.com/yourusername';
$html = file_get_html($url);
$dateTimes = array();
foreach ($html->find('div.GridTimeline .js-short-timestamp') as $value) {
$dateTimes[] = $value->innertext;
}
Result of print_r($dateTimes);:
Array
(
[0] => 2h
[1] => 2h
[2] => 2h
// Truncated...
[10] => 11h
[11] => May 30
[12] => May 30
[13] => May 6
// Truncated...
)
I was able to get the date using this code, tho I think there is a better way. I think it would be best to find a simple dom code that gets the text of the date-time in line
<span class"js-short-timestamp js-relative-timestamp" date-time="89393748474">
but instead I used two "list" php lines as seen below and that worked.
$dateTimes = array();
foreach ($html->find('div.GridTimeline .js-short-timestamp') as $value) {
$dateTimes[] = $value->outertext;
}
// These are the lines I get the date-time from.
list($Gone,$Keep) = explode("data-time=\"", $dateTimes[0]);
list($Date,$Gone) = explode("\"", $Keep);
$Date = date('M d, Y', $Date);
You want to use:
$html->find( "[data-time]", 0 );
In case anyone landing here in 2021, following no 1 google search result:
Unless I misinterpreted your intention, you might achieve what you want using (with simplehtmldom):
$html->find('span[data-time]')->attr[data-time];
The official simplehtmldom documentation fails to mention that. However, https://stackoverflow.com/a/14456823/10050838 is one possible source.

HTML import to wordpress, set publish date to match filename

I have a collection of 16,000 html files that I'm uploading to wordpress. I'm using HTML import 2. The date for the articles is located in two spots, neither of which can be detected by the plugin:
1.) each file is titled mmddyyxxxxxxx.htm
2.) the date exists in the same format in a paragraph at the bottom of the page, but surrounded by varying text. format: (yyyy, mm, dd)
and ideas?
Easiest solution here would be to use the "set timestamps to last time the file was modified." option while importing. Since the filenames have stamps in their dates, you can write a simple script to make the timestamps match. This can be done in bash, or PHP with the touch() function.
You may need to break up your files in usable groups, since glob() has a limit, but, here's a simple example to accomplish this:
<?php
# change mod+access times based on filenames
$files = glob("myfiles/*.htm");
foreach( $files as $file ) {
$temp = pathinfo( $file ); // may have relative path in it
$name = $temp['filename']; // just "mmddyyxxxxxxx" at this point
// assuming date format in filenames are fixed-lengths, you can rebuild
// timestamp to yyyy-mm-dd format with this:
$date = sprintf("20%s-%s-%s", // cheap trick to start years with 20
substr( $name, 4, 2 ),
substr( $name, 2, 2 ),
substr( $name, 0, 2 )
);
$stamp = strtotime( $name ); // timestamp
touch( $file, $stamp, $stamp ); // sets both mod + access time
}
?>
In case that your date format in the filename isn't fixed, you may need to get more creative.

Saving text box input to XML or txt file in HTML

I'm working on a HTML page project where I have 2 text boxes and basically I want to save the input data that the user put in the text boxes. What we did in my C# class was that we saved all input into a XML file so I'm assuming there is a similar way? Either to a XML or some other file that can store text?
Anyone that knows a solution?
I recommend the following php script
<?php
// check that form was submitted
// (you'll need to change these indices to match your form field names)
if( !empty( $_POST['firstname'] ) && !empty( $_POST['lastname'] ) ){
// remove html tags from submission
// (since you don't want them)
$firstname = strip_tags( $_POST['firstname'] );
$lastname = strip_tags( $_POST['lastname'] );
// create the date
// (you can change the format as desired)
$date = date( 'Y-m-d' );
// create an array that holds your info
$record = array( $firstname,$lastname,$date );
// save the record to your .txt file (I still recommend JSON)
$json = json_encode( $record );
$file = '/_server_/path/to/yourfile.txt';
file_put_contents( $json,$file );
}

Ada: Writing output numbers in a file separated by a tab

Just putting part of some code here where I am writing two values to a text file.
Ada.Long_Float_Text_IO.Put (File => Output_File, Item => Out_2, Fore => 1, Aft => 4, Exp => 0);
Ada.Text_IO.Put (Output_File, " ");
Ada.Long_Float_Text_IO.Put (File => Output_File, Item => Out_3, Fore => 1, Aft => 4, Exp => 0);
I can separate these numbers Out_2 and Out_3 by a space as shown in the code. The results give (after writing more numbers in the two columns):
-75.2340 421.5700
1256.0000 15.4700
-4568.9800 -118.2800
3784.2100 0.0000
I would like to know if there is a way to specify a tab spacing so that I can have something like this in my text file:
-75.2340 421.5700
1256.0000 15.4700
-4568.9800 -118.2800
3784.2100 0.0000
So which control character produces the above alignment?
Thanks a lot...
For a tab, there’s the obsolescent ASCII.HT or Ada.Characters.Latin_1.HT.
Or you could use the Width parameter to Ada.Long_Float_Text_IO.Put and friends.
Edit: There is no Width parameter for real output! You could use a large Fore, which would effectively right-justify the output.
Instead of that intervening:
Ada.Text_IO.Put (Output_File, " ");
Call the Set_Col procedure instead, which moves the output line position to the specified column. E.g.
Ada.Text_IO.Set_Col(Output_File, 13);

Best way to find illegal characters in a bunch of ISO-889-1 web pages?

I have a bunch of html files in a site that were created in the year 2000 and have been maintained to this day. We've recently began an effort to replace illegal characters with their html entities. Going page to page looking for copyright symbols and trademark tags seems like quite a chore. Do any of you know of an app that will take a bunch of html files and tell me where I need to replace illegal characters with html entities?
You could write a PHP script (if you can; if not, I'd be happy to help), but I assume you already converted some of the "special characters", so that does make the task a little harder (although I still think it's possible)...
Any good text editor will do a file contents search for you and return a list of matches.
I do this with EditPlus. There are several editors like Notepad++, TextPad, etc that will easily help you do this.
You do not have to open the files. You just specify a path where the files are stored and the Mask (*.html) and the contents to search for "©" and the editor will come back with a list of matches and when you double click, it opens the file and brings up the matching line.
I also have a website that needs to regularly convert large numbers of file names back and forth between character sets. While a text editor can do this, a portable solution using 2 steps in php was preferrable. First, add the filenames to an array, then do the search and replace. An extra piece of code in the function excludes certain file types from the array.
Function listdir($start_dir='.') {
$nonFilesArray=array('index.php','index.html','help.html'); //unallowed files & subfolders
$filesArray = array() ; // $filesArray holds new records and $full[$j] holds names
if (is_dir($start_dir)) {
$fh = opendir($start_dir);
while (($tmpFile = readdir($fh)) !== false) { // get each filename without its path
if (strcmp($tmpFile, '.')==0 || strcmp($tmpFile, '..')==0) continue; // skip . & ..
$filepath = $start_dir . '/' . $tmpFile; // name the relative path/to/file
if (is_dir($filepath)) // if path/to/file is a folder, recurse into it
$filesArray = array_merge($filesArray, listdir($filepath));
else // add $filepath to the end of the array
$test=1 ; foreach ($nonFilesArray as $nonfile) {
if ($tmpFile == $nonfile) { $test=0 ; break ; } }
if ( is_dir($filepath) ) { $test=0 ; }
if ($test==1 && pathinfo($tmpFile, PATHINFO_EXTENSION)=='html') {
$filepath = substr_replace($filepath, '', 0, 17) ; // strip initial part of $filepath
$filesArray[] = $filepath ; }
}
closedir($fh);
} else { $filesArray = false; } # no such folder
return $filesArray ;
}
$filesArray = listdir($targetdir); // call the function for this directory
$numNewFiles = count($filesArray) ; // get number of records
for ($i=0; $i<$numNewFiles; $i++) { // read the filenames and replace unwanted characters
$tmplnk = $linkpath .$filesArray[$i] ;
$outname = basename($filesArray[$i],".html") ; $outname = str_replace('-', ' ', $outname);
}