I have an issue: I have to convert HTML file to docx, the important thing is that i have large html file so PhpOffice\PhpWord does not help.
Also I have a second option: I have docx file but i have to change something in there. I tried this:
$templateProcessor = new TemplateProcessor('ref2.docx');
$templateProcessor->setValue(['{{name}}', '{{spec}}', '{{email}}'], [$array[0], $array[1], $array[2]]);
$templateProcessor->saveAs($array[0].'_WORD.docx');
but it didn't work (it worked for the first parameter, but the last one..)
What should i do to make it work correctly?
For multiple values replacement, by referring to the documentation, try replace your code with the correct function and syntax
From
$templateProcessor->setValue(['{{name}}', '{{spec}}', '{{email}}'], [$array[0], $array[1], $array[2]]);
To
$templateProcessor->setValues([
'{{name}}' => $array[0],
'{{spec}}' => $array[1],
'{{email}}' => $array[2]
]);
Hope it helps!
Related
$requestParams = [ 'aame_uuid' => 'aba627', 'currency' => 'TEST'];
ksort($requestParams);
$hashString = http_build_query($requestParams);
var_dump( $hashString);
gives
string(30) "aame_uuid=aba627¤cy=TEST"
so why does currency transformed to ¤cy , how can correct it ?
I think the problem is the ¤ html code.
http://character-code.com/currency-html-codes.php ¤ gets replaced to this char.
Maybe you should add an specific seperator like &
to the http_build_query to make sure it will replaced to foo.php?aaaa_uid=aba627¤cy=Test
I just tested your code and I get the following string back:
C:\wamp64\www\test\test.php:8:string 'aame_uuid=aba627¤cy=TEST' (length=30)
It seems to be working fine for me. Have you tried using some other key? If it gives somewhat the same result your file or server could be corrupt.
If not the word curren might be presaved as a function to do something (which is highly unlikely). Did you mistype it as currentcy maybe (current is a php function)?
This is a tricky one...I am trying to replace some strings in a file that i hold in array.
Because there are a lot of files...i've been trying to find the fastest way possible.
I tried this (which worked) but it was slow.
First parsed all the files and got an array of the values i want to
change (lets say 500).
Then I wrote a foreach loop to parse through the files one by one.
Then inside that, another foreach loop to go through the values one by one
preg_replacing the file for any occurrences of the array value.
This takes forever though cause not all files need to be parsed with 500 array elements.
So i am changing the code now like this:
Parse every file and make an array of the values i want to replace.
Search the file again for all the occurrences for each array value and replace it.
Save the file
I think this will be much faster that the old way...The problem i am having though now is with the read/write loop, and the array loop...
I want to do this as fast as possible...cause there will be a lot of files to parse and some have 100+ values.
So far i got this in a function.
function openFileSearchAndReplace($file)
{
$holdcontents = file_get_contents($file);
$newarray = makeArrayOfValuesToReplace($holdcontents);
foreach ($newarray as $key => $value) {
$replaceWith = getNewValueFor($value);
$holdcontent = preg_replace('/\\b'.$value.'\\b/', $replaceWith, $holdcontents);
}
file_put_contents($file, $holdcontent, LOCK_EX); //Save and close
}
Now, this doesnt work...it just changes 1 value only because i have file_put_contents and file_get_contents outside of the foreach. (Not to mention that it replaces values that it shouldnt replace. Probably cause the read/write are outside of the loop.) I have to put them inside to work..but thats gonna be slow..cause it take 3-4seconds per file to do the change since there are a lot of elements in the array.
How can i "Open the file", "Read it", "Change ALL values first", "Then save close the file", so i can move to the next.
EDIT:
Maybe i am not explaining it well i dont know...or is this too complicated....I have to parse the array of values...there is no way i can avoid that...but instead of (In every loop), i open the file search and replace 1 value, close the file.....I want to do this:
Open the file, get the content in an array or string or whatever. For all the values i have keep replacing the text with the equivalent value, and when all the values are done...that array or string write to the file. So i am only opening/closing the file once. Instead of waiting for php to read/write/close all the time.
-Thanks
How about just using str_replace(mixed $search , mixed $replace , mixed $subject)?
You can have an array of search strings which will be replaced by their corresponding item in the replace array and as the PHP manual says:
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace().
Also just close the file and reopen it with mode 'w'. File will be truncated to 0 length
Added Edit
$fileContents = file_get_contents("theFile");
$search = array('apples', 'oranges');
$replace = array('pears', 'lemons');
$newContents = str_replace($search, $replace, $fileContents);
$handle = fopen("theFile","w");
fwrite($handle, $newContents);
fclose($handle);
That's it your file has all the old strings replaced with new ones.
There is no solution to the problem. file_get_contents and file_put_contents simply doesnt work like that.
I appreciate everyone's attention to the problem.
I have an HTML file that I have downloaded using curl and inserted into a string. The HTML file has a lot of content but I am looking to parse a certain section of the document and insert this section into an array. The tricky part of this is that the section that I'm trying to parse is NOT HTML, it's code in JavaScript block:
<!-- script block -->
<script type="text/javascript" src="//external.site.com/76b07.js"></script>
<script>....code.....
"235533":{"itemId":"235533","type":"0","image":{"url":"thispic.jpg"}:"summary":"This Item"},
"235534":{"itemId":"235534","type":"1","image":{"url":"thisotherpic.jpg"}:"summary":"This Other Item"},
</script>
How can I import item information as an array?:
$array = array( "itemId" => "235533", "type" => "0", "image" => "thispic.jpg", "summary" =>"This Item" );
You might use a RegExp to match "....":{....} located between <script> tags. The strings, you're interested in, are JSON variables.
Once you have each json variable in a string, you could try with json_decode()
$json_string = '"235533":{"itemId":"235533","type":"0","image":{"url":"thispic.jpg"}:"summary":"This Item"}';
$json = json_decode($json_string);
$myArray = (array)$json;
Try json_decode function in php
You would first need to figure out how to isolate the data structure using whatever string searching methodologies you can use that are repeatable even when the data changes. It is hard to say what this might be without further context from you about the content around the data structure - i.e. what is the same in all cases, and what varies.
You would then eventually get the data strings and json_decode them as others have suggested.
use regex to match them
preg_match_all('/[0-9]+":{"itemId":"(?P<itemId>[0-9]*)","type":"(?P<type>[0-9]{1})","image":{"url":"(?P<image>.*)"}:"summary":"(?P<summary>.*)}/',$mystring,$elements,PREG_SET_ORDER);
then loop through $elements to get your values
Use explode. For example, something like
$array = explode('","', $string);
that would get close to what you want.
Edit:
This looks like a better fit for you.
Here is a template of a possible text file I might need to import into my database:
#NAME:"Test"
#REV:"rev1"
#PRODUCT:"product1","description1","option1"
#PRODUCT:"product2","description2","option1","option2"
"A1","key1","DALI"
"B1","key2",""
"B2","key3","option2"
"C1","key4",""
The first 4 lines is a new addition to the format of these files. I was importing the comma separated data itself successfully before the addition of the comment lines on top.
I was wondering if someone can provide me the most efficient way to put all the values in the comment lines into variables in PHP.
I always have a little trouble when it comes to RegEx. I'm not sure how to best grab the lines starting with a #.
Essentially, I would like to have the following data available to me:
$csv['name']: "Test";
$csv['rev']: "rev1";
$csv['products']: array(
0 => array('name' => "product1", 'desc' => "description1", 'options' => "option1"),
1 => array('name' => "product2", 'desc' => "description2", 'options' => "option1,option2"),
);
$csv['data']: The rest of the data in text file
There could be multiple #PRODUCTS defined, so that is why it would be nice to have an array made from those lines.
Thanks for your help.
Are you using php 5.3? If so, then you can simply read your file using fgets() and detect comments using substr($line, 0, 1). If you don't detect a #,it means it a data line, then pass it on to str_getcsv()...
Cheers
To match something started with #, just use ^ at the beginning of regexp (outside of group)
I have a big excel file that looks like this:
I would like to put each row into an array.
Is this possible to access the first row's order id like this?
$result[0][2] // returns 7432
Assuming the actual first row that gives prefix for the columns' name is not present.
How could I do that?
I recommended to use PHPEXCEL library
https://github.com/PHPOffice/PHPExcel
you can see an example
Update:
Now the alternative to this library is phpspreadsheet
Save the spreadsheet as a CSV, then use PHP's built-in CSV functions. See the sample code here:
http://php.net/manual/en/function.fgetcsv.php
May be my answer is too simple (for one time work only), but I use the CONCATENATE "Function" in excell.
The last cell on each row will have concatenation function, like this:
=CONCATENATE("['";A2;"'=>['data1' => '";B2;"', 'data2' => '";C2;"'],")
where:
column "A" is ID of something;
column "B" is first characteristic;
column "C" is second characteristic;
etc.
Then just copy and paste function results to Your script or config file, and do not forget the first and the last bracket.
This works for me:
$content = file_get_contents($your_file_path);
$lines = array_map("rtrim", explode("\n", $content));
Since the PHPExcel library deprecated they've released "PhpSpreadsheet"
This will help PhpSpreadsheet