Bibtex php preg_match_all - php

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.

Related

Iterate through a php multi dimensional array with specific condition

I have a php array below and i want to know how to get number of companies who did a training course. Look below:
Array
(
[0] => Array
(
[date_creation] => Apr 10, 2021 10:17 pm
[idformation] => 84
[idsociete] => 7
[training] => ELECTRICAL SAFETY TRAINING
[company] => ALUCAM
)
[1] => Array
(
[date_creation] => Apr 10, 2021 10:55 pm
[idformation] => 84
[idsociete] => 7
[training] => ELECTRICAL SAFETY TRAINING
[company] => ALUCAM
)
[2] => Array
(
[date_creation] => Apr 12, 2021 03:27 pm
[idformation] => 104
[idsociete] => 201
[training] => FORKLIFT, JLG SCISSOR LIFT, AERAL PLATFORM
[company] => US EMBASSY
)
);
Each array represents the record of a worker in the database from a company say Alucam and did training Electrical safety.
So from the array above i want to get something like:
2 Alucams did electrical safety as seen in the array.
I just need a clue on how to get the count of persons who did a particular training from the array.
Please help
I assume you can have the same training from different companies, opposite case you can simplified the code.
Input data (I simplified your input array, including only the fields I need):
$workers = array(array("training" => "ELECTRICAL SAFETY TRAINING", "company" => "ALUCAM"),
array("training" => "ELECTRICAL SAFETY TRAINING", "company" => "ALUCAM"),
array("training" => "FORKLIFT, JLG SCISSOR LIFT, AERAL PLATFORM", "company" => "US EMBASSY"),
array("training" => "FORKLIFT, JLG SCISSOR LIFT, AERAL PLATFORM", "company" => "ALUCAM")
);
Php code:
$trainingCount = array();
foreach($workers as $worker) {
$training = $worker["training"];
$company = $worker["company"];
if(! array_key_exists($training, $trainingCount)) {
$trainingCount[$training] = array();
}
if(! array_key_exists($company, $trainingCount[$training])) {
$trainingCount[$training][$company] = 0;
}
$trainingCount[$training][$company]++;
}
Result:
array('ELECTRICAL SAFETY TRAINING' => array('ALUCAM' => 2), 'FORKLIFT, JLG SCISSOR LIFT, AERAL PLATFORM' => array('US EMBASSY' => 1, 'ALUCAM' => 1));
Effectively you have a list of employees with their training listed in a comma separated list?
So basically you need to iterate through the list stripping out the information you require (company & training). Then every time you get a match you increment the matching data.
There are a few ways to do this the simplest would be to iterate through the results to create an array which looks something like...
$countArray = [
"Alucam" => [
"ELECTRICAL SAFETY TRAINING" = 2,
],
];
The code would look like:
$countArray = [];
// Generate the array
foreach ($array as $employee) {
$trainingList = array_map("trim", explode(",", $employee["training"]));
foreach ($trainingList as $training) {
$countArray[$employee["company"]][$training] = ($countArray[$employee["company"]][$training] ?? 0) + 1;
}
}
// Generate the output
foreach ($countArray as $companyName => $training) {
foreach ($training as $trainingName => $trainingCount) {
echo "{$trainingCount} {$companyName} did {$trainingName}", PHP_EOL;
}
}
/*
Output:
2 ALUCAM did ELECTRICAL SAFETY TRAINING
1 US EMBASSY did FORKLIFT
1 US EMBASSY did JLG SCISSOR LIFT
1 US EMBASSY did AERAL PLATFORM
*/
However, this does mean you can have "unusual" characters in array keys which could lead to problems further down the line. So you may do better with a slightly more complicated approach (i.e. having index arrays for the company and training names) which gives an array a little something like...
$countArray = [
'company' => [
0 => 'ALUCAM',
1 => 'US EMBASSY',
],
'training' => [
0 => 'ELECTRICAL SAFETY TRAINING',
1 => 'FORKLIFT',
2 => 'JLG SCISSOR LIFT',
3 => 'AERAL PLATFORM',
],
'count' => [
0 => [
0 => 2,
],
1 => [
1 => 1,
2 => 1,
3 => 1,
],
],
];
The code would look like:
// Generate the array
foreach ($array as $employee) {
if (false === ($companyIndex = array_search($employee["company"], $countArray["company"]))) {
$companyIndex = count($countArray["company"]);
$countArray["company"][] = $employee["company"];
}
$trainingList = array_map("trim", explode(",", $employee["training"]));
foreach ($trainingList as $training) {
if (false === ($trainingIndex = array_search($training, $countArray["training"]))) {
$trainingIndex = count($countArray["training"]);
$countArray["training"][] = $training;
}
$countArray["count"][$companyIndex][$trainingIndex] = ($countArray["count"][$companyIndex][$trainingIndex] ?? 0) + 1;
}
}
// Generate the output
foreach ($countArray["count"] as $companyKey => $companyCount) {
$companyName = $countArray["company"][$companyKey];
foreach ($companyCount as $trainingKey => $trainingCount) {
$trainingName = $countArray["training"][$trainingKey];
echo "{$trainingCount} {$companyName} did {$trainingName}", PHP_EOL;
}
}
You can use array_count_values and array_column to achieve something like this: You can modify as required.
$arr = [
['date_creation' => 'Apr 10, 2021 10:17 pm', 'idformation' => 84, 'idsociete' => 7, 'training' => 'ELECTRICAL SAFETY TRAINING', 'company' => 'ALUCAM'],
['date_creation' => 'Apr 10, 2021 10:17 pm', 'idformation' => 84, 'idsociete' => 7, 'training' => 'ELECTRICAL SAFETY TRAINING', 'company' => 'ALUCAM'],
['date_creation' => 'Apr 12, 2021 03:27 pm', 'idformation' => 104, 'idsociete' => 201, 'training' => 'FORKLIFT, JLG SCISSOR LIFT, AERAL PLATFORM', 'company' => 'US EMBASSY'],
];
$training = 'ALUCAM';
$companies = array_count_values(array_column($arr, 'company'))[$training]; // outputs: 2

Realurl generating unwanted language codes inside url

I'm again stuck up with a realurl trouble.. This time it's worse than ever. I've configured realurl_conf for L parameter as well as for other parameters.
'preVars' =>
array (
array(
'GETvar' => 'no_cache',
'valueMap' => array(
'nc' => 1,
),
'noMatch' => 'bypass',
),
array (
'GETvar' => 'L',
'valueMap' => array (
'en' => '0', // Default
'it' => '5', // Italian
'de' => '7', // German
),
'noMatch' => 'bypass',
),
),
URLs are generating perfectly as expected.. But for some pages the urls are being shown like
mydomain.ch/de/nr/page1/innerpage
innerpage's typoScript template is like,
config.tx_realurl_enable = 1
# language configuration
page.config.linkVars = L
page.config.sys_language_uid = 7
page.config.language = de
page.config.locale_all = de_DE.utf8
page.config.htmlTag_langKey = de
# english language
[globalVar = GP:L = 0]
page.config.sys_language_uid = 0
page.config.language = en
page.config.locale_all = en_GB.utf8
page.config.htmlTag_langKey = en
[global]
Any one have any idea, what is happening? (The confusing part is like 'nr' is not even configured anywhere!)
OMG !!! Some how alternative page title was there for a page which is 'nr'. That is shown in url. Wasted around 2 hours, checking other configurations. So.. Next time, honestly guyz.. Check page properties.
Slipped this time. (y)

PHP return array of matching KEY NAMES and there values

I'm having a terrible time trying to pull out some values out of an array. Here is a slimmed down version of the array.
$properties = array( array( PropertyID => 2845,
Address_1 => "1234 Any street",
MEDIA_IMAGE_00 => "23428.jpg",
MEDIA_IMAGE_TEXT_00 => "Front of House",
MEDIA_IMAGE_01 => "29872.jpg",
MEDIA_IMAGE_TEXT_01 => "Master Bedroom",
MEDIA_IMAGE_02 => "29834.jpg"
),
array( PropertyID => 2845,
Address_1 => "555 This street",
MEDIA_IMAGE_00 => "234234.jpg",
MEDIA_IMAGE_TEXT_00 => "Front of House",
MEDIA_IMAGE_01 => "298724.jpg",
MEDIA_IMAGE_TEXT_01 => "Second Bedroom",
MEDIA_IMAGE_02 => "298346.jpg"
),
array( PropertyID => 2845,
Address_1 => "333 Main street",
MEDIA_IMAGE_00 => "2342845.jpg",
MEDIA_IMAGE_TEXT_00 => "Lounge",
MEDIA_IMAGE_01 => "2987246.jpg",
MEDIA_IMAGE_TEXT_01 => "Front of House",
MEDIA_IMAGE_02 => "2983434.jpg"
),
);
There is a massive amount of data in each sub array I've trimmed it down for length...
I'm inserting this data into a MySQL database, however, I'm inserting the images into a separate table [PropertyID, Image, ImageText] because some properties may have more images than others.
So now that the background is out of the way.
How do I pull just the keys of the array that match and there info into another array? So that I would end up with an array from the above that would end up with something similar to:
$property_images = array( array( PropertyID => 2845,
IMAGE => "23428.jpg",
IMAGE_TEXT => "Front of House"),
array( PropertyID => 2845,
IMAGE => "29872.jpg",
IMAGE_TEXT => "Master Bedroom",
array( PropertyID => 2845,
MEDIA_IMAGE_02 => "29834.jpg"
IMAGE_TEXT => "Living Room"
I've tried sscanf to no avail and fiddle around with array_keys but haven't managed to figure out how to target the key names rather than the key values...
Thanks for your help in advance!
Edit:
foreach( $properties as $v ) {
foreach( $v as $k => $m ) {
if( strpos($k, 'MEDIA_IMAGE_TEXT_') !== FALSE ) {
$new_array[] = array('PropertyID' => $v['PropertyID'], 'IMAGE'=>$prev, 'IMAGE_TEXT'=>$m );
}
if( strpos($k, 'MEDIA_IMAGE_') !== FALSE ) {
$prev = $m; //var_dump($prev);
}
}
}

Applying a custom order to a multi-dimensional array

I have this array
$arr = array(
'one' => array(
'slidertitle' => 'lorem ipsum',
'sliderlocation' => 'http://localhost/images/1.jpg',
'sliderdescription' => 'this is a good lorem ipsum image',
'sliderposition' => 1
),
'two' => array(
'slidertitle' => 'second slider',
'sliderlocation' => 'http://localhost/images/2.jpg',
'sliderdescription' => 'this space was reserved for a link source code here',
'sliderposition' => 2
),
'six' => array(
'slidertitle' => 'sixth slider',
'sliderlocation' => 'http://localhost/images/6.jpg',
'sliderdescription' => 'this is the sixth slider,like,really!',
'sliderposition' => 6
)
);
which i need to look like this
$arr = array(
'two' => array(
'slidertitle' => 'second slider',
'sliderlocation' => 'http://localhost/images/2.jpg',
'sliderdescription' => 'this space was reserved for a link source code here',
'sliderposition' => 2
),
'six' => array(
'slidertitle' => 'sixth slider',
'sliderlocation' => 'http://localhost/images/6.jpg',
'sliderdescription' => 'this is the sixth slider,like,really!',
'sliderposition' => 6
),
'one' => array(
'slidertitle' => 'lorem ipsum',
'sliderlocation' => 'http://localhost/images/1.jpg',
'sliderdescription' => 'this is a good lorem ipsum image',
'sliderposition' => 1
)
);
I am attempting to do that by defining the expected array structure and introducing a dummy array.I then chunk the array and merge each chunk to the array format and i plan to finally unset the dummy and i am left with the array i want and in the order i want.
$arrayFormat = array(
'dummy' => array(
'slidertitle' => 'xxxx',
'sliderlocation' => 'xxxxxxx',
'sliderdescription' => 'xxxxxx',
'sliderposition' => 0
)
);
$arrayLength = count($arr);
$afterChunk = array_chunk($arr,$arrayLength);
$one = $afterChunk[0][0];
$two = $afterChunk[0][1];
$mergedArray = array_merge($arrayFormat,$one);
$secondMergedArray = array_merge($mergedArray,$two);
echo '<pre>';
print_r($secondMergedArray);
echo '</pre>';
The problem is array_chunk() does not include the key of the array so i am getting
Array (
[dummy] => Array
(
[slidertitle] => xxxx
[sliderlocation] => xxxxxxx
[sliderdescription] => xxxxxx
[sliderposition] => 0
)
[slidertitle] => second slider
[sliderlocation] => http://localhost/images/2.jpg
[sliderdescription] => this space was reserved for a link source code here
[sliderposition] => 2 )
when i print_r($secondMergedArray);.is there something that can be done to array_chunk() to include the array key or is there any other array function that can help me get individual array inclusive of the key?.
It's really hard to tell what you're wanting in terms of how to sort the elements. You've not been very clear in the question. There has to be something in the array that you know what order it needs to be.
In the absence of any clues as to what that is, I'm going to assume you want to specify the order of the array keys manually.
So, the current array is array('one'=>... , 'two'=>... , 'six'=>... ) and you want to sort those keys in an order you want to specify manually.
The solution is to use the uksort() function, along with a separate array specifying your sort order:
$arr = ... //input array as specified in the question
$sortOrder = array('two','one','six');
uksort($arr, function ($a, $b) use ($sortOrder) {
$sortMe = array_flip($sortOrder);
if ($sortMe[$a] == $sortMe[$b]) { return 0; }
return ($sortMe[$a] < $sortMe[$b]) ? -1 : 1;
});
print_r($arr);
Outputs your array in 'two','one','six' order. Change the $sortOrder array as required.
Hope that helps.
Note: the syntax I've provided above only works in PHP 5.3 and above. (if you're using an older version, you need to upgrade)
use uksort() for custom order for multidimensional array
http://php.net/manual/en/function.uksort.php

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