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)
Related
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!
I've been working on a very large, complicated framework I'm writing. I have used a process similar to this one for two other packages, and it worked flawlessly, however this str_replace is giving me trouble.
This is a small snippet from a much larger function. The function this particular snippet is from replaces the default value in a config file to a value dependent on user input. I'm not going to include all the other various string replacements that go on in this function, nor am I going to detail the script which grabs the contents of the config file and writes it. The issue is isolated to this snippet. I know so because this snippet replicates the issue in the larger function when it is isolated in a test file.
The equivilent code is...
<?php
$new_table_array = '$ecom_tables = [
"product" => "test1",
"promo" => "test2",
"records" => "test3"
];';
$test = str_replace("$ecom_tables = [];", $new_table_array, '
$ecom_tables = [];
');
?>
That snippet is formatted identically to the real function. The result of this snippet in the function, and in the test file is...
$ecom_tables$ecom_tables = [
"product" => "test1",
"promo" => "test2",
"records" => "test3"
];
I have looked over my concatenations, and insured that all of my string quotations are in the correct place. It passes the syntax check, and also runs when I load the test page, however the result is always unexpected.
Why is str_replace only replacing part of the string I specified with the new value?
If I have a handlebar merge variable like {{message}} in my template, how can I have it render HTML output if given the following in PHP:
array(
'name' => 'message',
'content' => '<p>First paragraph.</p><p>Second paragraph.</p>'
)
Right now it outputs the content without parsing the paragraph tags.
If you're using Handlebars I think the proper way to do it is with triple braces, e.g:
{{{html_content}}}
I'm not sure it is ok to mix mc:edit with Handlebars in Mandrill:
Combining Handlebars with either mc:edit regions or merge tags in a single message isn't supported. You should pick Handlebars or mc:edit regions plus merge tags.
https://mandrill.zendesk.com/hc/en-us/articles/205582537-Using-Handlebars-for-dynamic-content
Actually when sending using mandrill the message variable has a field merge_language and when you change to this
'merge_language' => 'handlebars'
It works. For your case i think the value is default 'mailchimp' hence the need to use mc:edit
To answer my own question, I just added mc:edit="message" to the div containing the message, like this:
<div mc:edit="message"></div>
I then added this to my structure:
$template_content = array(
array(
'name' => 'message',
'content' => '<p>First paragraph.</p><p>Second paragraph.</p>'
),
);
I am trying to assign a variable in watir method after reading the text file. I am using the following method. In loop, first loop is working fine but in second loop it shows error.
My code is as follows:
file = File.new("states.txt", "r")
contentsArray=[] # start with an empty array
file.each_line {|line|
contentsArray.push line
}
browser = Watir::Browser.new
browser.goto 'http://example.com'
for y in 0..2 do
puts state=contentsArray[y]
browser.text_field(:name => 'Keyword').set 'Pediatrics'
browser.text_field(:name => 'Address').set "#{state}"
browser.div(:id => 'uniform-formSearchDoctorBtn').fire_event :click
browser.link(:class => "start-over pull-right gen-button").click
end
following error occurred .
`assert_exists': unable to locate element, using {:id=>"uniform-formSearchDoctorBtn", :tag_name=>"div"} (Watir::Exception::UnknownObjectException)
Where as when I am putting an static array in place of contentsArray
contentsArray=Array['AL','AK','AZ','AR']
Then code works fine.
Yes Got the solution with the help of justin.
if you output the contentsArray, you will likely get
["AL\n", "AK\n", "AZ\n", "AR"]
Notice that there is a "\n", which is the line break character. You want to strip the line break by changing contentsArray.push line to
contentsArray.push line.strip
For more details, see the question How do I remove carriage returns with Ruby? – Justin Ko
My current implementation, which is array based stores keys and values in a dictionary, example:
$arr = array(
'message' => 'Paste a flickr URL below.',
);
I realize that it was probably a bad idea storing html inside of a string such as this, but if I'm using gettext then in my .mo/.po files how should I handle storing a similar string? Should I just store words, such as 'Paste a' and 'URL below' and 'flickr' separately?
You should store something like
"Paste a %1 URL below"
and replace all 'vars' using something simple like str_replace('%1', $link, $message);
$link can also be translatable
"%1"
although that might be overkill (does flickr translate between languages?)
rationale behind this is that different languages have different grammatical structures and the ordering of the words wont always be the same.
Update:
as #alex and #chelmertz mention in the comments, try using the sprintf function, which is built for this very thing.
I'd go for this:
$arr = array(
'message' => _('Paste a %s URL below.'),
);
Having all translations as string literals within gettext function calls allows to use standard tools to update *.po catalogues.