replicate d3 indented tree to ms word using phpdocx - php

http://bl.ocks.org/mbostock/1093025
Hi, we can used to above link for reference as you can see it is an indented tree in d3. What i want to do is to replicate that tree in ms word using phpdocx. Here is what i have tried so far. I'm using a text with a border to replicate the rectangle in d3 and a line. I want my LINE at the back of my TEXT so that i can replicate the d3 indented tree. I used the position: absolute and z-index in the line but stil i have problem layouting my TEXT AND LINE to phpdocx i wish i can an image of what i have so far i dont have enough privelege to do so
below code is what i have so far
<?php
require_once 'classes/CreateDocx.inc';
$docx = new CreateDocx();
$array = array('young girl rape our young girls but violeta gave willingly gosh shes nervous','young girl rape our young girls but violeta gave willingly gosh shes nervous','young girl rape our young girls but violeta gave willingly gosh shes nervous','young girl rape our young girls but violeta gave willingly gosh shes nervous');
$a = ($k * 10) + 10;
$b = ($k * 10) + 60;
$options = array('points' => "$a,$a $a,$b $b,$b",
'strokecolor' => '#ff00ff',
'strokeweight' => '2',
'position' => 'absolute',
'fillcolor' => 'none',
'z-index' => -500
);
$docx->addShape('polyline', $options);
foreach($array as $k => $v){
$indentLeft = $k * 250;
$paragraphOptions = array( 'border' => 'single',
'position' => 'absolute',
'borderColor' => 'b70000',
'borderWidth' => 12,
'backgroundColor' => '#F4F4F4',
'indentLeft' => $indentLeft,
'spacingBottom' => 50,
'fontSize' => 10,
);
$docx->addText($v, $paragraphOptions);
}
$docx->createDocx('example_addTextBox_1');
?>

Related

Can PHPword show Pie Chart in percenage with two decimals?

I use PHPword class to create Word file in PHP.
Can you create Pie chart to show value in percentage with two decimals?
$c3 = array('Expensive kW', 'Cheap kW');
$s3 = array($expensive, $cheap);
$tablePie2Charts = $section->addTable('Chart');
$tablePie2Charts->addRow();
$stylePie2Chart = array(
'width' => Converter::inchToEmu(5),
'height' => Converter::inchToEmu(3),
'valueAxisTitle' => 'Last month consumed in kW',
'showLegend' => true,
'dataLabelOptions' => array(
'showCatName' => false,
'showVal' => false,
'showPercent' => true
)
);
$c1 = $tablePie2Charts->addCell()->addChart('pie', $c3, $s3, $stylePie2Chart);
I also encountered the same problem.But i can't find an api to support it.
And here my solution:
edit the file /PhpOffice/Phpword/Writer/Word2007/Part/Chart.php
near the line 250. Insert the code
$xmlWriter->writeElementBlock("c:numFmt",['formatCode'=>'0.00%','sourceLinked'=>'0']);
between
$xmlWriter->startElement('c:dLbls');
and
foreach ($style->getDataLabelOptions() as $option => $val) {
It finally look like that:
$xmlWriter->startElement('c:dLbls');
$xmlWriter->writeElementBlock("c:numFmt",['formatCode'=>'0.00%','sourceLinked'=>'0']);
foreach ($style->getDataLabelOptions() as $option => $val)
if you just want it in pie chart and you should:
if ('pie' === $this->element->getType()){
$xmlWriter->writeElementBlock("c:numFmt",['formatCode'=>'0.00%','sourceLinked'=>'0']);
}
You can change the formatCode like 0.0% 0.00% 0.000% whatever you want.
If someone find the better way,plz tell me,thanks.

Keyword matching in an array/string of keywords using PHP

I have a piece of PHP code as follows:
$words = array(
'Artist' => '1',
'Tony' => '2',
'Scarface' => '3',
'Omar' => '4',
'Frank' => '5',
'Torrentino' => '6',
'Mel Gibson' => '7',
'Frank Sinatra' => '8',
'Shakes' => '9',
'William Shakespeare' => '10'
);
$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";
$re = '/\b(?:' . join('|', array_map(function($keyword) {
return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';
preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
echo $keyword, " ", $words[$keyword], "\n";
}
The code returns the below:
William Shakespeare 10 artist Artist 1 Frank 5 Frank 5
The code works well to not echo the partial keywords such as 'Shakes' => '9' in Shakespeare. However, as you can see the code can not detect 'Frank Sinatra' => '8' as a keyword as it is in Frank Sinatra was an American singer, also artist does not have any value (i.e. 1). Could you please help me to change the code somehow to echo William Shakespeare 10 artist 1 Artist 1 Frank 5 Frank 5 Frank Sinatra 8 Frank Sinatra 8 instead of William Shakespeare 10 artist Artist 1 Frank 5 Frank 5 in the current version. thanks for your help.
I've managed to achieve result:
William Shakespeare 10 artist 1 Artist 1 Frank Sinatra 8 Frank Sinatra
8
using the code:
<?php
mb_internal_encoding('UTF-8');
$words = array(
'Artist' => '1',
'Tony' => '2',
'Scarface' => '3',
'Omar' => '4',
'Frank' => '5',
'Torrentino' => '6',
'Mel Gibson' => '7',
'Frank Sinatra' => '8',
'Shakes' => '9',
'William Shakespeare' => '10'
);
uksort($words, function ($a, $b) {
$as = mb_strlen($a);
$bs = mb_strlen($b);
if ($as > $bs) {
return -1;
}
else if ($bs > $as) {
return 1;
}
return 0;
});
$words_ci = array();
foreach ($words as $k => $v) {
$words_ci[mb_strtolower($k)] = $v;
}
$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";
$re = '/\b(?:' . join('|', array_map(function($keyword) {
return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';
preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
echo $keyword, " ", $words_ci[mb_strtolower($keyword)], "\n";
}

Bibtex php preg_match_all

I have a text file with a Bibtex export.
The text file has a number of entries following the pattern below.
#article{ls_leimeister,
added-at = {2013-01-18T11:14:11.000+0100},
author = {Wegener, R. and Leimeister, J. M.},
biburl = {http://www.bibsonomy.org/bibtex/27bb26b4b4858439f81aa0ec777944ac5/ls_leimeister},
journal = {International Journal of Technology Enhanced Learning (to appear)},
keywords = {Challenges Communities: Factors Learning Success VirtualCommunity and itegpub pub_jml pub_rwe},
note = {JML_390},
title = {Virtual Learning Communities: Success Factors and Challenges},
year = 2013
}
I want to use php and considered preg_match_all
The following didnt get me anywhere:
preg_match_all('/#^.*}$/', file_get_contents($file_path),$results);
I wanted to start simple, but that didnt really work.
I am kinda new to php RegEx.
The perfect final output would be:
Array
(
[0] => Array
(
['type'] => article
['unique_name'] => ls_leimeister
['added-at'] => 2013-01-18T11:14:11.000+0100
['author'] => Wegener, R. and Leimeister, J. M.
['biburl'] => http://www.bibsonomy.org/bibtex/27bb26b4b4858439f81aa0ec777944ac5/ls_leimeister
['journal'] => International Journal of Technology Enhanced Learning (to appear)
['keywords'] => Challenges Communities: Factors Learning Success VirtualCommunity and itegpub pub_jml pub_rwe
['note'] => JML_390
['title'] => Virtual Learning Communities: Success Factors and Challenges
['year'] => 2013
)
[1] => Array
(
[...] => …
)
)
Try this : Here I have fetched only type and unique_name, by looking at it, you can fetch all others.
$str = '#article{ls_leimeister,
added-at = {2013-01-18T11:14:11.000+0100},
author = {Wegener, R. and Leimeister, J. M.},
biburl = {http://www.bibsonomy.org/bibtex/27bb26b4b4858439f81aa0ec777944ac5/ls_leimeister},
journal = {International Journal of Technology Enhanced Learning (to appear)},
keywords = {Challenges Communities: Factors Learning Success VirtualCommunity and itegpub pub_jml pub_rwe},
note = {JML_390},
title = {Virtual Learning Communities: Success Factors and Challenges},
year = 2013
}';
preg_match_all('/#(?P<type>\w+){(?P<unique_name>\w+),(.*)/',$str,$matches);
echo $matches['type'][0];
echo "<br>";
echo $matches['unique_name'][0];
echo "<br>";
echo "<pre>";
print_r($matches);
Output array format will be little different from yours, but you can change this format to yours.
Pattern: /^#([^{]+)\{([^,]+),\s*$|^\s*([^\R#=]+) = \{(.*?)}/ms (Demo)
This pattern has two alternatives; each containing two capture groups.
type and unique_name are captured and stored in elements [1] and [2].
all other key-value pairs are stored in elements [3] and [4].
This separated array storage allows reliable processing when constructing the desired output array structure.
Input:
$bibtex='#BOOK{ko,
title = {Wissenschaftlich schreiben leicht gemacht},
publisher = {Haupt},
year = {2011},
author = {Kornmeier, M.},
number = {3154},
series = {UTB},
address = {Bern},
edition = {4},
subtitle = {für Bachelor, Master und Dissertation}
}
#BOOK{nial,
title = {Wissenschaftliche Arbeiten schreiben mit Word 2010},
publisher = {Addison Wesley},
year = {2011},
author = {Nicol, N. and Albrecht, R.},
address = {München},
edition = {7}
}
#ARTICLE{shome,
author = {Scholz, S. and Menzl, S.},
title = {Alle Wege führen nach Rom},
journal = {Medizin Produkte Journal},
year = {2011},
volume = {18},
pages = {243-254},
subtitle = {ein Vergleich der regulatorischen Anforderungen und Medizinprodukte
in Europa und den USA},
issue = {4}
}
#INBOOK{shu,
author = {Schulz, C.},
title = {Corporate Finance für den Mittelstand},
booktitle = {Praxishandbuch Firmenkundengeschäft},
year = {2010},
editor = {Hilse, J. and Netzel, W and Simmert, D.B.},
booksubtitle = {Geschäftsfelder Risikomanagement Marketing},
publisher = {Gabler},
pages = {97-107},
location = {Wiesbaden}
}';
Method: (Demo)
$pattern='/^#([^{]+)\{([^,]+),\s*$|^\s*([^\R#=]+) = \{(.*?)}/ms';
if(preg_match_all($pattern,$bibtex,$out,PREG_SET_ORDER)){
foreach($out as $line){
if(isset($line[1])){
if(!isset($line[3])){ // this is the starting line of a new set
if(isset($temp)){
$result[]=$temp; // send $temp data to permanent storage
}
$temp=['type'=>$line[1],'unique_name'=>$line[2]]; // declare fresh new $temp
}else{
$temp[$line[3]]=$line[4]; // continue to store the $temp data
}
}
}
$result[]=$temp; // store the final $temp data
}
var_export($result);
Output:
array (
0 =>
array (
'type' => 'BOOK',
'unique_name' => 'ko',
'title' => 'Wissenschaftlich schreiben leicht gemacht',
'publisher' => 'Haupt',
'year' => '2011',
'author' => 'Kornmeier, M.',
'number' => '3154',
'series' => 'UTB',
'address' => 'Bern',
'edition' => '4',
'subtitle' => 'für Bachelor, Master und Dissertation',
),
1 =>
array (
'type' => 'BOOK',
'unique_name' => 'nial',
'title' => 'Wissenschaftliche Arbeiten schreiben mit Word 2010',
'publisher' => 'Addison Wesley',
'year' => '2011',
'author' => 'Nicol, N. and Albrecht, R.',
'address' => 'München',
'edition' => '7',
),
2 =>
array (
'type' => 'ARTICLE',
'unique_name' => 'shome',
'author' => 'Scholz, S. and Menzl, S.',
'title' => 'Alle Wege führen nach Rom',
'journal' => 'Medizin Produkte Journal',
'year' => '2011',
'volume' => '18',
'pages' => '243-254',
'subtitle' => 'ein Vergleich der regulatorischen Anforderungen und Medizinprodukte
in Europa und den USA',
'issue' => '4',
),
3 =>
array (
'type' => 'INBOOK',
'unique_name' => 'shu',
'author' => 'Schulz, C.',
'title' => 'Corporate Finance für den Mittelstand',
'booktitle' => 'Praxishandbuch Firmenkundengeschäft',
'year' => '2010',
'editor' => 'Hilse, J. and Netzel, W and Simmert, D.B.',
'booksubtitle' => 'Geschäftsfelder Risikomanagement Marketing',
'publisher' => 'Gabler',
'pages' => '97-107',
'location' => 'Wiesbaden',
),
)
Here is the site that I extracted new sample input strings from.

Parsing Text File into Variables with PHP

Need some help with parsing a text file into PHP. The file is generated by a PHP script, so I don't have control over the content formatting. The text file looks like this:
7/4/2013-7/4/2013 Best Legs in a Kilt To start the summer
off with a bang, the Playhouse has teamed up with the folks at The
Festival. kilt.jpg 1,1,0,
-
7/8/2013-7/23/2013 Hot Legs Yes, folks, it's all platform
shoes, leisure suits, and crazy hair-do's. hotstuff.jpg
1,1,0,
-
The code that I have thus far is:
$content = file_get_contents('DC_PictureCalendar/admin/database/cal2data.txt');
list($date, $showname, $summary, $image, $notneeded, $notneeded2) = explode("\n", $content);
echo 'Show Name' . $showname . '<br/>';
This only gets me the first show title, I need to grab all of them. I'm sure a For loop would do it, but not sure how to do it based on the contents of the file. All I need is the 2nd line (show title) and the 4th line (image). Any help? Thanks in advance.
If you are reading the entire file into an array anyway, then just use file() which will read each line into an array.
$content = file('DC_PictureCalendar/admin/database/cal2data.txt', FILE_IGNORE_NEW_LINES);
You can then filter all the lines you don't want like this
$content = array_diff($content, array('1,1,0', '-'));
You can then break into chunks of 4 lines each (i.e. one item per entry)
$content_chunked = array_chunk($content, 4);
This would give you an array like
Array(
0 => Array(
0 => '7/4/2013-7/4/2013',
1 => 'Best Legs in a Kilt',
2 => 'To start the summer off with a bang, the Playhouse has teamed up with the folks at The Festival.',
3 => 'kilt.jpg'
),
1 => Array(
0 => '7/8/2013-7/23/2013',
1 => 'Hot Legs',
2 => 'Yes, folks, it's all platform shoes, leisure suits, and crazy hair-do's.',
3 => 'hotstuff.jpg'
) ... etc.
)
I would then map this array into a useful array of objects with property names that are meaningful to you:
$items = array_map(function($array)) {
$item = new StdClass;
$item->date = $array[0];
$item->showname = $array[1];
$item->summary = $array[2];
$item->image = $array[3];
return $item;
}, $content_chunked);
That would leave you with an array of objects like:
Array(
0 => stdClass(
'date' => '7/4/2013-7/4/2013',
'showname' => 'Best Legs in a Kilt',
'summary' => 'To start the summer off with a bang, the Playhouse has teamed up with the folks at The Festival.',
'image' => 'kilt.jpg'
),
1 => stdClass(
'date' => '7/8/2013-7/23/2013',
'showname' => 'Hot Legs',
'summary' => 'Yes, folks, it's all platform shoes, leisure suits, and crazy hair-do's.',
'image' => 'hotstuff.jpg'
) ... etc.
)

how to replace text in a mysql database content array

im trying to get rid of unneccesary text in my database content.My code looks like this:
if(mysql_num_rows($result))
$items[] = array();
while($row = mysql_fetch_assoc($result)) {
$items[] = array('id' => $row['id'], 'cat' => $row['cat'], 'type' => $row['type'], 'name' => $row['name'], 'sub_title' => $row['sub_title'], 'display_date' => $row['display_date'], 'slug' => $row['slug'], 'ticket_url' => $row['ticket_url'], 'status' => $row['status'], 'content' => $row['content'], 'display_until' => $row['display_until'], 'photo' => $row['photo'], 'thumb' => $row['thumb']);
$removals = array('\n','\r','\t','<\/div>\r\n');
$spaces = "";
$parsedText = str_replace($removals, $spaces, $items);
}
echo json_encode(array('events'=>$items));
And the content then displays like this:
{"events":[[],{"id":"66","cat":"9","type":"2","name":"Oileán - A Celebration of the Blasket Islands","sub_title":"National Folk Theatre","display_date":"Tues 4th - Thurs 6th May at 8.30pm","slug":"This production celebrates life on the Blasket Islands in times past, exploring the way of life of the islanders and their spirit of survival. Oileán captures the essence of this island community, their traditions and customs, their wealth of song and story, their love of life and their strong kinship with one another. ","ticket_url":"","status":"1","content":"
\r\n\tPresented by the members of the National Folk Theatre of Ireland</strong>, this production celebrates and explores Blasket Island living while also challenging our own notions of identity as contemporary islanders. </div>\r\n
\r\n\t </div>\r\n
\r\n\tPremiered in 2003, Oileán</strong></em> marked the 50th</sup> anniversary of the departure of the Blasket Islanders to the mainland. The Great Blasket Island, located off the coast of West Kerry still retains an almost mystical significance for many, both from Ireland and abroad. The way of life of the islanders and their spirit of survival is framed in this production, which captures the essence of this island community, their traditions and customs, their wealth of song and story, their love of life and their strong kinship with one another. </div>\r\n
\r\n\t </div>\r\n
\r\n\tOileán</i></b> is delivered in the unique Siamsa style through the medium of dance, mime, music and song.</div>\r\n
\r\n\t </div>\r\n
\r\n\t
\r\n\t\t </div>\r\n\t
\r\n\t\tPlease note that due to the popularity of performances by the National Folk Theatre</strong>, some productions may be sold out well in advance and tickets may not be available on-line. However, we often have returns and tickets may be available nearer to the day of a performance</strong>. Please contact us directly by phone on: +353 (0)66 7123055.</em></div>\r\n\t
\r\n\t\t </div>\r\n\t
\r\n\t\t </div>\r\n</div>\r\n","display_until":"20100504","photo":"1269869378-oilean_side.jpg","thumb":"1269869378-oilean_thumb.jpg"},
The above display is the first item in the DB.
Im trying the replace all the \r , \n , etc in the above content?How can i go about this?Is what i have allready on the right track?
2 things
if(mysql_num_rows($result))
$items = array(); // not $items[], that would set the first item as an array
while($row = mysql_fetch_assoc($result)) {
$removals = array("\n","\r","\t","<\/div>\r\n");
$spaces = "";
$items[] = array(
'id' => $row['id'],
'cat' => $row['cat'],
'type' => $row['type'],
'name' => $row['name'],
'sub_title' => $row['sub_title'],
'display_date' => $row['display_date'],
'slug' => $row['slug'],
'ticket_url' => $row['ticket_url'],
'status' => $row['status'],
// replace the content here
// youll want to use preg_replace though otherwise youll end up with multiple </div>'s
'content' => str_replace( $removals, $spaces, $row['content'] ),
'display_until' => $row['display_until'],
'photo' => $row['photo'],
'thumb' => $row['thumb']
);
}
echo json_encode(array('events'=>$items));

Categories