PHP Smalot PdfParser get certain section - php
I need help about PHP Smalot\PdfParser. https://github.com/smalot/pdfparser
Does anybody know how to get or access certain section.
Example. Invoice and want to get access to items/products object section as Object/Array
getText method works but retrieves all the text on invoice.
Thanks alot!
You could use some loop like this:
$metaData = $pdf->getDetails(); //Gets PDF metadata
$xtargetTextCoordinate = "12.345" ///////////USE YOUR OWN
$ytargetTextCoordinate = "678.90" ///////////USE YOUR OWN
//Going through each PDF's page...
for ($x=0 ; $x < $metaData['Pages']; $x++ ){
//Reset variables
$streamOfThisPage = [];
$streamOfThisPage = $pdf->getPages()[$x]->getDataTm();
$targetText= "";
//Going through each key element of this page...
foreach($streamOfThisPage as $arrayEle){
if( ($arrayEle[0][4] == $xtargetTextCoordinate ) && ($arrayEle[0][5] == $ytargetTextCoordinate ) ){ //X & Y depend of your document structure...
$targetText = "";
//Remove unecessary data if any
$thisRowWords = explode(" " , $arrayEle[1] );
$referencePeriod = $thisRowWords[0];
foreach($thisRowWords as $position => $word){
$targetText = $targetText . $word . " ";
}
}
}
echo $targetText;
Related
How to create a php variable with text and value of another variable
Want to create a php variable using text before and after the value of another variable. variable variables. But have only seen examples of assignment with no text. $vsBOA_W[]=$rows['vsBOA_W']; // BOA = team 3-char abbreviation. Looking for something similar to above but insert 3-char abbreviations based on a input file. $numOfTeams = 3; // Determined from external source $teamAbbr = array("BOA","CAA","CHN"); // For simplicity for this example. This array would normally be created from an external source. for($i=0; $i<$numOfTeams; $i++) { // I know I can use size of array instead of nunOfTeams. That's not the issue. echo $teamAbbr[$i]."<br>"; // for testing $$("vs".{'$teamAbbr[$i]'}."_W[]"} = $rows['$$("vs".{'$teamAbbr[$i]'}."_W"}']; // a total guess } I expect the end result to look like: $vsBOA_W[]=$rows['vsBOA_W']; for BOA Update #2: I tried the following (breaking down each step) and get the same error on $$TeamWins assignment. for($i=0; $i<$numOfTeams; $i++) { echo $teamAbbr[$i]."<br>"; $TeamWins = 'vs' . $teamAbbr[$i] . '_W'; echo "TeamWins=$TeamWins<br>"; $TeamWinsHold = $rows[$TeamWins]; echo "TeamWinsHold=$TeamWinsHold<br>"; $$TeamWins[] = $TeamWinsHold; } Update #3: for($i=0; $i<$numOfTeams; $i++) { echo $teamAbbr[$i]."<br>"; $TeamWins = 'vs' . $teamAbbr[$i] . '_W'; echo "TeamWins=$TeamWins<br>"; $TeamWinsHold = $rows[$TeamWins]; echo "TeamWinsHold=$TeamWinsHold<br>"; ${$TeamWins}[] = $TeamWinsHold; } foreach(${$TeamWins} as $value) { echo "value=$value<br>"; // only displays last element or value assigned from above loop. } Update #4 (final): $teamW = array(); $teamL = array(); for($i=0; $i<$numOfTeams; $i++) { //echo $teamAbbr[$i]."<br>"; $teamWName = 'vs' . $teamAbbr[$i] . '_W'; $teamLName = 'vs' . $teamAbbr[$i] . '_L'; //echo "teamWName=$teamWName<br>"; //echo "teamLName=$teamLName<br>"; $teamW[$teamWName] = $rows[$teamWName]; $teamL[$teamLName] = $rows[$teamLName]; }
I don't quite understand the interplay with the rows in your example. But going by your guess assignment, you can always simplify, by forming the variable name upfront: <?php $rows = ['xFOOy'=>[], 'xBARy'=>[], 'xBAZy'=>[]]; $items = ['FOO', 'BAR', 'BAZ']; foreach($items as $abbr) { $name = 'x' . $abbr . 'y'; ${$name}[] = $rows[$name]; } But, I'd say you'd be better off with a keyed array than variable variables, as it makes for easier inspection, and there is less chance of namespace clashes.
looping through txt file to use specific part of a string
I am new to Php and can't seem to figure this out no matter how much I've googled. So I've opened the txt file (which consists of multiple lines of this type of string unique Identifier IMEI in bold: Rx:00:39:54 06/09/2015:+RESP:GTEPS,210101,863286020022449,,8296,01,1,3,0.0,0,1031.1,29.367950,-30.799161,20150906003710,,,,,,2857.9,20150906003710,8038$) There are different strings with different IMEIs but i only want to use a specific one. My question is, how do I extract/only use the string with the same Unique identifier and then loop through those to use in another function? My function has different cases and each case has different calculations, so I'll need to loop through the txt file (with e.g. 863286020022449 as Identifier, ignoring other identifiers/IMEIs) in order to use the string in my function as below: This is my starter function: function GetParam($unknownFunction, $numberCommas) { $returnString = ""; $foundSting = 0; $numberFound = 0; $len = strlen($unknownFunction); for ($i = 0; $i < $len; ++$i) { if ($Rawline[$i] == ",") { ++$numberFound; if ($numberFound > $numberCommas) break; if ($numberFound == $numberCommas) $foundSting = 1; } else if ($foundSting == 1) { $returnString .= $unknownFunction[$i]; } } return $returnString; echo $returnString; } $i = strpos($unknownFunction, ":GT"); $p = substr($unknownFunction, $i+3,3); $Protocol = GetParam($unknownFunction, 1); //this switch reads the differences in the message types (e.g. HBD- in this case is a heartbeat message type and would thus have a different amount of commas in the string and has different definitions of the characters within the commas) switch ($p) { case 'HBD': //+ACK:GTHBD,220100,135790246811220,,20100214093254,11F0$ //This is an example of an HBD message $result2["Type"] = 'Heart beat'; $IMEI = GetParam($unknownFunction, 2); $mDate = GetParam($unknownFunction, 4); $mDate = substr($mDate,0,4).'-'.substr($mDate,4,2).'- '.substr($mDate,6,2).' '.substr($mDate,8,2).':'.substr($mDate,10,2).':'.substr($mDate,12,2); break; This is the biggest problem I am facing at the moment and when I print the different lines, it indicates the correct IMEI but it does not loop through the whole file to use each string that belongs to that IMEI. Your assistance would be greatly appreciated. Thank you so much. Example of input file: Rx:00:00:00 28/02/2018:+RESP:GTFRI,3C0103,862045030241360,,14067,11,1,1,29.7,320,151.1,30.949307,-29.819685,20180227235959,0655,0001,013A,87B6,00,35484.1,01500:51:31,,,100,220101,,,,20180228000000,3461$ Rx:00:00:01 28/02/2018:+RESP:GTERI,380201,869606020047340,gv65,00000002,14076,10,1,1,119.0,119,24.3,18.668516,-34.016808,20180227235955,0655,0001,00F7,2DC9,00,98912.0,02235:20:25,0,100,220101,0,0,20180227235958,FF20$ Rx:00:00:03 28/02/2018:+RESP:GTERI,380201,869606020162990,,00000002,12912,10,1,1,0.0,230,1127.3,30.846671,-27.674206,20180227235956,0655,0001,013E,88B0,00,106651.1,03546:44:42,0,100,210101,0,0,20180227235959,6190$ Rx:00:00:03 28/02/2018:+ACK:GTHBD,450102,865084030005340,gb100,20180228000003,CC61$ Rx:00:00:03 28/02/2018:+RESP:GTERI,380201,869606020115980,,00000002,13640,10,1,1,12.1,353,1663.1,28.580726,-28.162208,20180227235957,,,,,,37599.6,02422:07:24,0,100,220101,0,0,20180228000000,1937$ Rx:00:00:04 28/02/2018:+RESP:GTERI,380502,869606020276840,gv65,00000002,12723,10,1,1,0.0,106,1232.8,22.878013,-27.951762,20180227235952,0655,0001,0204,63C5,00,13808.9,00778:32:20,0,100,210100,0,0,20180228000002,2C50$ Rx:00:00:04 28/02/2018:+RESP:GTERI,380502,869606020274530,gv65,00000002,12683,10,1,1,0.0,91,1213.7,24.863444,-28.174319,20180227235956,0655,0001,0203,69F1,00,9753.2,00673:49:21,0,100,210100,0,0,20180228000003,8AC7$ Rx:00:00:05 28/02/2018:+ACK:GTHBD,380201,863286023083810,,20180228000003,0D87$ Rx:00:00:06 28/02/2018:+RESP:GTFRI,3C0103,862045030241360,,14086,10,1,1,34.0,327,152.0,30.949152,-29.819501,20180228000002,0655,0001,013A,87B6,00,35484.1,01500:51:36,,,100,220101,,,,20180228000005,3462$ Rx:00:00:06 28/02/2018:+ACK:GTHBD,060228,862894021626380,,20180228000007,F9A5$ Rx:00:00:07 28/02/2018:+RESP:GTERI,380201,869606020019430,,00000002,12653,10,1,1,0.0,219,1338.7,26.882063,-28.138099,20180228000002,,,,,,86473.7,05645:48:34,0,93,210101,0,0,20180228000003,0FA5$ Rx:00:00:09 28/02/2018:+ACK:GTHBD,380502,869606020233940,gv65,20180228000008,7416$ Rx:00:00:10 28/02/2018:+RESP:GTAIS,380201,869606020171710,,11,11,1,1,0.0,95,281.2,30.855164,-29.896575,20180228000009,0655,0001,0156,9A9F,00,156073.7,20180228000008,F9A4$ Each GT message means something which is why i need to extract only one specific IMEI and use the result in my function as a breakdown of what every set of numbers between the commas actually mean. The end result needs to be populated in an excel spreadsheet but that's a different issue.
Nested foreach, keeping tracking of the IMEIs you've already gone through. Or something like this. <?php $filename = 'info.txt'; $contents = file($filename); foreach ($contents as $line) { $doneAlreadyArray = array(); $IMEI = GetParam($line, 2); foreach ($contents as $IMEIline){ $thisIMEI = GetParam($IMEIline,2); //check if already done the IMEI previously if (!in_array($thisIMEI, $doneAlreadyArray)){ //matching IMEIs? if ($thisIMEI == $IMEI){ //run new function with entire $IMEIline new_function($IMEIline); } } } //add IMEI to doneAlreadyArray array_push($doneAlreadyArray,$IMEI); } ?>
If I've understood your question right and you want to extract the string(line) with the same Unique identifier, this may be useful for your needs as a strating point. The example is very basic, and use data from your question: <?php // Read the file. $filename = 'input.txt'; $file = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Each item of $output will contain an array of lines: $output = array(); foreach ($file as $row) { $a = explode(',', $row); $imei = $a[2]; if (!array_key_exists($imei, $output)) { $output[$imei] = array(); } $output[$imei][] = $row; } // Then do what you want ... foreach ($output as $key=>$value) { echo 'IMEI: '.$key.'</br>'; foreach($value as $row) { // Here you can call your functions. I just echo the row: echo $row.'</br>'; } } ?>
thank you for the feedback. Ryan Dewberry ended up helping me. The fix was simpler than I thought too :) //Unknownfunction is now $line function GetParam($line, $numberCommas) { $returnString = ""; $foundSting = 0; $numberFound = 0; $len = strlen($line); for ($i = 0; $i < $len; ++$i) { if ($line[$i] == ",") { ++$numberFound; if ($numberFound > $numberCommas) break; if ($numberFound == $numberCommas) $foundSting = 1; } else if ($foundSting == 1) { $returnString .= $line[$i]; } } return $returnString; // print $returnString; } //this is new - makes sure I use the correct IMEI $contents = file($fileName); foreach ($contents as $line){ $haveData = 0; $IMEI = GetParam($line, 2); if ($IMEI == $gprsid){ $i = strpos($line, ":GT"); $p = substr($line, $i+3,3); $Protocol = GetParam($line, 1); //this is the part I struggled with as well - This is an array of all of my //calculation //results and in printing it out I can see that everything is working $superResult = array(); array_push($superResult,$result2); print_r($superResult); } } Much appreciated. Thank you!
How to work with or merge two separate arrays of keywords in PHP
I have a recurring need to merge two arrays of information. This is happening very often and run possibly 20+ times per page load, so I want to ensure I am attacking this in the most speed-friendly efficient way. The function receives a list of text keywords and creates a simple unique array of the keywords. The challenge is then taking these keywords, and getting their associated filenames.html from a database (I used medoo, but that's irrelevant) and be able to present 2 different link types based on the result, which are : A) where the filename.html exists (or) B) to a .php file that can handle the keyword as a query (i.e., a static file for the keyword doesn't exist so instead send the user to a .php?=the_keyword to present an appropriate page). I am somewhat confident that I have made this more elongated that it could be - This is how I am approaching this so far: $unique_best_keywords = array_unique($best_keywords); $keywords_to_display = array(); $keywords_to_find = array(); foreach ($unique_best_keywords as $keyword) { unset($keyword_to_clean); if (in_array($keyword, $flagged_keywords, TRUE) !== TRUE) { $keyword_to_clean = strtolower(trim($keyword)); $keyword_to_clean = preg_replace('/[^a-z0-9-]/', '-', $keyword_to_clean); $keyword_to_clean = preg_replace('/-+/', "-", $keyword_to_clean); $keywords_to_display[] = array ( "anchor_text" => trim($keyword), "file_name" => $keyword_to_clean); } } The example result here would be anchor_text: "jobs at apple" and file_name: "jobs-at-apple". The keyword with dashes in it, is the index in my database to query for matching static html files - so I approach this like the following: if (isset($keywords_to_display) && !empty($keywords_to_display)) { foreach ($keywords_to_display as $keyword_to_call_from_db) { $keywords_to_find[] = $keyword_to_call_from_db['file_name']; } unset($keyword_topic_view_links); $keyword_topic_view_links = $database->select("my_database_table", array( "filename_dot_html"), array("unique_filename_index" => $keywords_to_find)); // medoo db query } } if (isset($keyword_topic_view_links) && is_array($keyword_topic_view_links)) { $view_links = array(); foreach ($keyword_topic_view_links as $a_link) { $view_links[] = $a_link['filename_dot_html']; } } if (isset($keyword_topic_view_links) && isset($keywords_to_display)) { foreach ($keywords_to_display as $keyword) { if (count($view_links) < 1) { // Send all keywords to the .php file to present a nice page -- dynamic! unset($keyword_display,$keyword_display_file); $keyword_display = $keyword['anchor_text']; $keyword_display_file = $keyword['file_name']; $html_output .= '' . $keyword_display . ' '; } else { // Send some topics views to static html pages-- unset($keyword_display,$keyword_display_file,$keyword_display_file_html); $keyword_display = $keyword['anchor_text']; $keyword_display_file = $keyword['file_name']; $keyword_display_file_html = "$keyword_display_file.html"; if (in_array($keyword_display_file_html, $view_links) !== TRUE) { // Keyword WAS NOT in the db array! $html_output .= '' . $keyword_display . ' '; } else { // Keyword WAS IN in the db array! Yay! $html_output .= '' . $keyword_display . ' '; } } } } Any advice / optimisations would be greatly appreciated.
Not able to remove backslash from below code?
below is my code the issue is that when i enter any username with apostrophe (') Additional character "\ backslash" is being displayed when Search results are returned. Below is my code i find that a function addslashes is used in the checkusername function so backslash is getting added. if ( 0 < count( $my_field_place )) { for ( $i = 0; $i < count( $my_field_place ); $i++ ) { if ( true === isset( $Fields[$i] )) { print "gMapping[$i] = new MappingItem( '" . addslashes( $Fields[$i] ) . "', '" . checkusername( $my_field_place[$i] ) . "' );"; } } } function checkusername($inStr) { $orig = array(); $new = array(); $orig[00] = "/\n/`" ; $new[00] = "\\?n"; $orig[01] = "/[^\x-*()]/"; $new[01] = ""; $var1 = preg_replace($orig, $new, $inStr); $var2 = addslashes($var1 ); // i am not sure why addslashes is used but i am asked not to remove because of security reasion? return $var2; } Note: I google and find that it used for security reason Since in my case we are only displaying the searched result. So i am not sure why this function is used here. My fix is to add stripslashes() function before returning which will removes backslashes added by the addslashes() function. Please find the code snippet and the comment for code change below: function checkusername($inStr) { $orig = array(); $new = array(); $orig[00] = "/\n/`" ; $new[00] = "\\?n"; $orig[01] = "/[^\x-*()]/"; $new[01] = ""; $var1 = preg_replace($orig, $new, $inStr); $var2 = addslashes($var1); return stripslashes($var2); // i am not sure stripslashes is correct fix or not? } Please help is it fine to added stripslashes or is there any other way to handle it ?
You can restrict user from those special characeter though in gmail ,yahoo,fb etc they will never allow this character.since t allows multiple words to be represented in a somewhat readable manner below are some doc https://support.google.com/a/answer/33386?hl=en see second guidelines
continued : unable to post fields to the next page in php and HTML
So I have fields that are generated dynamically in a different page and then their results should posted to story.php page. fields is going to be : *noun1 *noun2 *noun3 and story is going to be : somebody is doing *noun1 etc. What I want to do is to replace *noun1 in the story with the *noun, I have posted from the previous page ( I have *noun1 posted from the previous page ) but the code below is not working : $fields = $_POST['fields']; $story = $_POST['story']; $fieldsArray = split(' ', $fields); for ($i = 0; $i < count($fieldsArray); $i++) { ${$fieldsArray[$i]} = $_POST[$fieldsArray[$i]]; } // replace words in story with input for ($i = 0; $i < count($story); $i++) { $thisWord = $story[$i]; if ($thisWord[0] == '*') $story[$i] = ${$thisWord.substring(1)}; } $tokensArray = split(' ',$tokens); echo $story;
Your problem is likely that you are trying to echo $story, which I gather is an array. You might have better luck with the following: $storyString = ''; for ($i = 0; $i < count($story); $i++) { $storyString .= $story[i] . ' '; } echo $storyString; echo can't print an array, but you can echo strings to your heart's content.
You almost certainly don't want variable variables (e.g. ${$fieldsArray[$i]}). Also, $thisWord.substring(1) looks like you're trying to invoke a method, but that's not what it does; . is for string concatenation. In PHP, strings aren't objects. Use the substr function to get a substring. preg_replace_callback can replace all your code, but its use of higher order functions might be too much to get into right now. For example, function sequence($arr) { return function() { static $i=0 $val = $arr[$i++]; $i %= count($arr); return $val; } } echo preg_replace_callback('/\*\w+/', sequence(array('Dog', 'man')), "*Man bites *dog."); will produce "Dog bites man." Code sample requires PHP 5.3 for anonymous functions.