I want to show list of journals and their abbreviation like:
Journal Name, Abbreviation
I am getting the data I need from :
http://images.webofknowledge.com/WOK46/help/WOS/D_abrvjt.html
So I am running the following:
$ch = curl_init();
//Set options
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://images.webofknowledge.com/WOK46/help/WOS/A_abrvjt.html'
));
$result = curl_exec($curl);
curl_close($curl);
$data=json_decode($result, true);
//!End function, make_call
But now what it shows me is the whole page, but as I said I only need the name of the journals(dt) and the abbreviation (dd). So How can I pars the result?
HTML DOM parsing via Simple HTML DOM
Scraping method ...
<?php
Function Scraper($file, $cnt = NULL) {
/*
#param $file, url or path/file
#param $cnt, (number of results to list) empty for all, or number
*/
require_once('PATH/TO/simple_html_dom.php');
//set_time_limit(0); // uncomment for large files
$result = array();
// Create DOM from URL
$html = file_get_html($file);
IF ($html) {
IF (empty($cnt)) { $cnt = count($html->find('DT')); }
foreach($html->find('DL') as $dl) {
for ($i = 0; $i < $cnt; $i++) {
$dt = $dl->find('DT', $i)->plaintext;
$dd = $dl->find('DD', $i)->plaintext;
$result[] = array(trim($dt) => trim($dd));
}
}
}
return $result;
}
$array = Scraper('http://somesite.com/page.html');
print_r($array);
?>
Example output ...
Array
(
[0] => Array
(
[D H LAWRENCE REVIEW] => D H LAWRENCE REV
)
[1] => Array
(
[D-D EXCITATIONS IN TRANSITION-METAL OXIDES] => SPRINGER TR MOD PHYS
)
[2] => Array
(
[DADOS-REVISTA DE CIENCIAS SOCIAIS] => DADOS-REV CIENC SOC
)
[3] => Array
(
[DAEDALUS] => DAEDALUS
)
[4] => Array
(
[DAEDALUS] => DAEDALUS-US
)
[5] => Array
(
[DAGHESTAN AND THE WORLD OF ISLAM] => SUOMAL TIED TOIM SAR
)
)
Updated example specific to user350082's issue ...
The definition lists DT and DD tags were not closed resulting in the dd being included in the find('dt') result.
<DT>D H LAWRENCE REVIEW<B><DD> D H LAWRENCE REV</B>
<DT>D-D EXCITATIONS IN TRANSITION-METAL OXIDES<B><DD> SPRINGER TR MOD PHYS</B>
etc. etc. etc.
Updated Function ...
Function Scraper($file, $cnt = NULL) {
/*
#param $file, url or path/file
#param $cnt, (number of results to list) empty for all, or number
*/
require_once('PATH/TO/simple_html_dom.php');
//set_time_limit(0); // uncomment for large files
$result = array();
// Create DOM from URL
$html = file_get_html($file);
IF ($html) {
foreach($html->find('DL') as $dl) {
IF (empty($cnt)) { $cnt = count($html->find('DT')); } // set count if null
for ($i = 0; $i < $cnt; $i++) {
$dd = $dl->find('DD', $i)->plaintext;
$dt = $dl->find('DT', $i)->innertext; // dt with html tags, easier for removing dd duplication
$dt = preg_replace('/\s+/', ' ',$dt); // remove extra whitespace, tabs etc.
// strip DD text duplication from DT
IF (($pos = strrpos($dt ,$dd)) !== false) {
$strlen = strlen($dd);
$dt = substr_replace($dt, "", $pos, $strlen);
}
$dt = strip_tags($dt); // remove html tags
IF (empty($dt)) { $dt = $dd; } // make sure dt is not empty
$result[] = array(trim($dt) => trim($dd));
}
}
}
return $result;
}
Related
Country.csv
this is countries.csv file, and i want to extract all the timezones from it, which is its 14th colomn, and the data in there is not properly json formatted. I'm trying to parse the json but it failed. Actually, I want to create an array of timezones like this
[0] => {zoneName:'Asia -> Kabul',gmtOffset:16200,gmtOffsetName:'UTC+04:30',abbreviation:'AFT',tzName:'Afghanistan Time'}
[1] => {zoneName:'Europe -> Mariehamn',gmtOffset:7200,gmtOffsetName:'UTC+02:00',abbreviation:'EET',tzName:'Eastern European Time'}
[2] => {zoneName:'Europe -> Tirane',gmtOffset:3600,gmtOffsetName:'UTC+01:00',abbreviation:'CET',tzName:'Central European Time'}
[3] => {zoneName:'Africa -> Algiers',gmtOffset:3600,gmtOffsetName:'UTC+01:00',abbreviation:'CET',tzName:'Central European Time'}
[4] => {zoneName:'Pacific -> Pago_Pago',gmtOffset:-39600,gmtOffsetName:'UTC-11:00',abbreviation:'SST',tzName:'Samoa Standard Time'}
[5] => {zoneName:'Europe -> Andorra',gmtOffset:3600,gmtOffsetName:'UTC+01:00',abbreviation:'CET',tzName:'Central European Time'}
[6] => {zoneName:'Africa -> Luanda',gmtOffset:3600,gmtOffsetName:'UTC+01:00',abbreviation:'WAT',tzName:'West Africa Time'}
what i'm doing, is this in App\Http\Controllers\TestController::class is this
public function timezone(): void {
$data = [];
if (($open = fopen(__DIR__ . '/countries.csv', 'r + b')) !== FALSE) {
while (($singleRecord = fgetcsv($open, NULL, ',')) !== FALSE) {
$data[] = $singleRecord;
}
fclose($open);
}
$data = $this->removeCharacters($data, ['[', ']']);
$data = $this->removeCharacters($data, (array)'\/', " -> ");
// $data = $this->removeCharacters($data, (array)'{}', '');
// dd(explode('},', $data[33][14]));
// dd(explode('},', $this->longJson));
// dd(explode(',', str_replace(['{', '}'], '', $data[167][14])));
$singleArray = [];
$count = count($data);
$itemsArray = [];
for ($i = 1; $i < $count; $i++) {
$singleArray[] = explode('},', $data[$i][14]);
foreach ($singleArray as $item) {
foreach ($item as $singleItem) {
$itemsArray[] = $singleItem;
}
}
}
$itemsArray = array_unique($itemsArray);
$this->printFormattedData($itemsArray);
}
private function removeCharacters($hayStack, array $charsArray, $character = ''): array {
$tempArray = [];
foreach ($hayStack as $item) {
$tempArray[] = str_replace($charsArray, $character, $item);
}
return $tempArray;
}
private function printFormattedData($data): void {
echo '<pre>';
print_r($data);
echo '</pre>';
}
Using regexp its not perfect solution, but you can transform timezone data to correct json format using function like this:
public function fixJson(string $str): string {
return preg_replace(
'/(?<=(\{|\,))(\w+)(?=\:)/',
'"$2"',
str_replace("'", '"', $zoneRaw) // may not work properly, if values may contain apostroph symbols, but seems not actual for your case
);
}
So, use this function:
$this->fixJson($data[$i][14]); // returns json string
json_decode($this->fixJson($data[$i][14])); // returns json decoded array
See usage example here https://sandbox.onlinephpfunctions.com/c/88f21
Following code would do, what you aim.
Please do not forget to mark this answer as ACCEPTED and thumbs up if it solves your problem, so that the work of the developers who help is appreciated and other developers can see in question list, that your question has already an accepted answer.
$lines = file("countries.csv");
array_shift($lines); // remove the first line with column names
$searchReplace = ['\/' => '->'];
$search = array_keys($searchReplace);
$replace = array_values($searchReplace);
$jsonFormattedTimeZones = [];
foreach($lines as $line)
{
$line = trim(str_getcsv($line)[14], " []");
$line = str_replace($search, $replace, $line);
$jsonFormattedTimeZones[] = $line;
}
print_r($jsonFormattedTimeZones);
Good Day
I have an array containing data seperated by a comma:
array (
[0]=>Jack,140101d,10
[1]=>Jack,140101a,15
[2]=>Jack,140101n,20
[3]=>Jane,141212d,20
[4]=>Jane,141212a,25
[5]=>Jane,141212n,30
)
There is a lot of data and I would like the data to be set out as:
array(
[Jack]=>
[140101]
=>[d] =>10
=>[a] =>15
=>[n] =>20
)
My code:
foreach ($out as $datavalue) {
$dat = str_getcsv($datavalue,',');
$datevalue = substr($dat[1],2,-1);
$shiftvalue = substr($dat[1],-1);
$totalvalue = $dat[2];
$sval[$shiftvalue] = $totalvalue;
$dval[$datevalue] = $sval;
$opvalue = $dat[0];
$final[$opvalue] = $dval;
}
Now it seems the array is populated even if there is no data from the original string, so my output shows results for Jack on the other dates even though there was no data for him originally. Hope this makes sense. Could anyone point out or suggest a solution please?
As mentioned in the comments, explode is what you need. See this working here.
<?php
$input = array (
0 => 'Jack,140101d,10',
1 => 'Jack,140101a,15',
2 => 'Jack,140101n,20',
3 => 'Jane,141212d,20',
4 => 'Jane,141212a,25',
5 => 'Jane,141212n,30',
);
$result = array();
foreach ($input as $key => $value) {
$valueParts = explode(',',$value); // now valueparts is an array like ('Jack','140101d','10')
$namePart = $valueParts[0];
$idPart = substr($valueParts[1],0,-1); // we need to strip the letter from the id
$charPart = substr($valueParts[1],-1); // and the id from the letter
$nrPart = $valueParts[2]; // you could use intval() to make this an integer rather than a string if you want
// Now we fill the array
if(!array_key_exists($namePart, $result)) {
$result[$namePart] = array();
}
if(!array_key_exists($idPart, $result[$namePart])) {
$result[$namePart][$idPart] = array();
}
if(!array_key_exists($charPart, $result[$namePart][$idPart])) {
$result[$namePart][$idPart][$charPart] = $nrPart;
}
}
var_dump($result);
id like to break a css file to php array.
i have this code:
function parseCss($css_str) {
$css = array();
// TODO include external css
$css_str = preg_replace('/(#import\s+["\'].+["\'];)/', "", $css_str);
// Strip all line endings and both single and multiline comments
$css_str = preg_replace('/\s*(?!<\")\/\*+[^\*]+\*+\/(?!\")\s*/', "", $css_str);
$css_class = explode("}", $css_str);
while(list($key, $value) = each($css_class)){
$aCSSObj = explode("{", $value);
$cssSelector = strtolower(trim($aCSSObj[0]));
if($cssSelector){
// regular class not in media query
$cssprops[] = $cssSelector;
$a = explode(";", $aCSSObj[1]);
while(list($key, $val0) = each($a)){
if(trim($val0)){
$aCSSSub = explode(":", $val0);
$cAtt = strtolower(trim($aCSSSub[0]));
if(isset($aCSSSub[1])){
$aCSSItem[$cAtt] = trim($aCSSSub[1]);
}
}
}
if((isset($css[$cssSelector])) && ($css[$cssSelector])){
$aCSSItem = array_merge($css[$cssSelector], $aCSSItem);
}
$css[$cssSelector] = (isset($aCSSItem)) ? $aCSSItem : null ;
unset($aCSSItem);
}
if(strstr($cssSelector, ",") && !strstr($cssSelector, "#media")){
$aTags = explode(",", $cssSelector);
print_r($aTags);
foreach($aTags as $key0 => $value0){
$css[$value0] = $css[$cssSelector];
}
unset($css[$cssSelector]);
}
}
unset($css_str, $css_class, $aCSSSub, $aCSSItem, $aCSSObj);
return $css;
}
but that doesn't return me a css file with media queries like it supposed to be.
and if there is a ":" sign in the css value -for example an IE expression like that:
top:expression((-20+(document.documentElement.clientHeight ?document.documentElement.clientHeight/2:document.body.clientHeight/2)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop:document.body.scrollTop))+'px')
its giving back cutted code.
so the result should look like that:
Array
(
[selector] => array (
[prop] => value
[prop2] => value
)
[#media handheld,only screen and (max-width:320px)] => array(
[selector] => array(
[prop] => value
[prop2] => value
)
)
)
how do i make it work?
Solved that!
eventually I've used CSSTidy to parse the css, and it does the job beautifully!
http://csstidy.sourceforge.net/index.php
So I'm attempting to remove specific parameters from the URL query string that are predefined in an array. Here's what I have so far:
<?php
// Construct the current page URL
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$currentUrl = 'http://' . $host . $script . '?' . $params;
// Store all URL parameters into an array (HOST, PATH, QUERY, etc)
$url_params = array();
$url_params = parse_url($currentUrl);
// Create an array to store just the query string, breaking them apart
$params_array = explode('&', $url_params['query']);
// Array holding URL parameters that we want to remove
$params_to_remove = array("param1", "param2", "param3", "param4", "param5");
$location = 0;
// Loop through and remove parameters found in PARAMS_TO_REMOVE array
for($x = 0; $x < count($params_to_remove); $x++) {
if(in_array($params_to_remove[$x], $params_array)) {
$location = array_search($params_to_remove[$x], $params_array);
unset($params_array[$location]);
}
}
// Print array after unwanted parameters were removed
print_r($params_array);
echo '<br /><br />';
// Construct a new array holding only the parameters that we want
$clean_params_array = array();
for($z = 0; $z < count($params_array); $z++) {
if($params_array[$z] != '') array_push($clean_params_array, $params_array[$z]);
}
// Print clean array
print_r($clean_params_array);
echo '<br />';
// Construct the new URL
// If there are paramters remaining in URL reconstruct them
if(count($clean_params_array) > 0) {
$final_url = 'http://www.example.com' . $url_params['path'] . '?';
for($y = 0; $y < count($clean_params_array); $y++) {
$final_url .= $clean_params_array[$y] . '&';
}
// Trim off the final ampersand
$final_url = substr($final_url, 0, -1);
}
// No parameters in final URL
else $final_url = 'http://www.example.com' . $url_params['path'];
// Print final URL
echo '<br />' . $final_url;
?>
Here's the output:
Using http://www.example.com/test.php?apple&banana&orange¶m1&strawberry¶m2&pineapple
Array ( [0] => apple [1] => banana [2] => orange [4] => strawberry [6] => pineapple )
Array ( [0] => apple [1] => banana [2] => orange [3] => strawberry )
http://www.example.com/test.php?apple&banana&orange&strawberry
As you can see I'm losing the last parameter. I also feel as if I'm being too verbose...where am I going wrong?
$new_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".implode("&",array_diff(explode("&",$_SERVER['QUERY_STRING']),Array("param1","param2","param3","param4","param5")));
One-liner ;)
Although you'd probably be better off taking that Array(...) out of there and defining it as a variable beforehand, so it's easier to read.
<?php
/**
* generateURL()
*
* Sprava URL adresy
*
* #author stenley <stenley#webdev.sk>
* #version 1.4
*/
function generateURL() {
$GET = $_GET;
$QUERY_STRING = '';
$SCRIPT_NAME = substr(strrchr($_SERVER["SCRIPT_NAME"],"/"),1);
$num_args = func_num_args();
if($num_args>0 && $num_args%2==0) {
$args = func_get_args();
foreach($args as $index => $paramName) {
$paramName = trim($paramName);
if($index%2==0 && !empty($paramName)) {
$paramValue = trim($args[$index+1]);
if(array_key_exists($paramName, $GET) && empty($paramValue)) {
unset($GET[$paramName]);
} elseif(!empty($paramValue)) {
$GET[$paramName] = $paramValue;
}
}
}
}
foreach($GET as $param => $value) {
$QUERY_STRING .= $param."=".$value."&";
}
return $SCRIPT_NAME.((empty($QUERY_STRING)) ? '' : "?".substr($QUERY_STRING,0,-5));
}
?>
here is great function for managing URL address. usage is easy. here are some examples in Slovak language. but I think you will understand code samples. or I will translate it for you
sorry for my english
Part of the answer is in this line:
Array ( [0] => apple [1] => banana [2] => orange [4] => strawberry [6] => pineapple )
Note that $params_array[5] does not exist. Yet you try to read $params_array[5] when $z==5
(In your while loop you go through values $z = 0; => $z < 6; (count($params_array))
You can use Kolink's solution, or use a foreach loop to go through all the values:
foreach($params_array as $param) {
if($param != '') array_push($clean_params_array, $param);
}
I have an array of my inventory (ITEMS A & B)
Items A & B are sold as sets of 1 x A & 2 x B.
The items also have various properties which don't affect how they are distributed into sets.
For example:
$inventory=array(
array("A","PINK"),
array("A","MAUVE"),
array("A","ORANGE"),
array("A","GREY"),
array("B","RED"),
array("B","BLUE"),
array("B","YELLOW"),
array("B","GREEN"),
array("B","BLACK")
);
I want to redistribute the array $inventory to create $set(s) such that
$set[0] => Array
(
[0] => array(A,PINK)
[1] => array(B,RED)
[2] => array(B,BLUE)
)
$set[1] => Array
(
[0] => array(A,MAUVE)
[1] => array(B,YELLOW)
[2] => array(B,GREEN)
)
$set[2] => Array
(
[0] => array(A,ORANGE)
[1] => array(B,BLACK)
[2] => NULL
)
$set[3] => Array
(
[0] => array(A,GREY)
[1] => NULL
[2] => NULL
)
As you can see. The items are redistributed in the order in which they appear in the inventory to create a set of 1 x A & 2 x B. The colour doesn't matter when creating the set. But I need to be able to find out what colour went into which set after the $set array is created. Sets are created until all inventory is exhausted. Where an inventory item doesn't exist to go into a set, a NULL value is inserted.
Thanks in advance!
I've assumed that all A's come before all B's:
$inventory=array(
array("A","PINK"),
array("A","MAUVE"),
array("A","ORANGE"),
array("A","GREY"),
array("B","RED"),
array("B","BLUE"),
array("B","YELLOW"),
array("B","GREEN"),
array("B","BLACK")
);
for($b_start_index = 0;$b_start_index<count($inventory);$b_start_index++) {
if($inventory[$b_start_index][0] == 'B') {
break;
}
}
$set = array();
for($i=0,$j=$b_start_index;$i!=$b_start_index;$i++,$j+=2) {
isset($inventory[$j])?$temp1=$inventory[$j]:$temp1 = null;
isset($inventory[$j+1])?$temp2=$inventory[$j+1]:$temp2 = null;
$set[] = array( $inventory[$i], $temp1, $temp2);
}
To make it easier to use your array, you should make it something like this
$inv['A'] = array(
'PINK',
'MAUVE',
'ORANGE',
'GREY'
);
$inv['B'] = array(
'RED',
'BLUE',
'YELLOW',
'GREEN',
'BLACK'
);
This way you can loop through them separately.
$createdSets = $setsRecord = $bTemp = array();
$bMarker = 1;
$aIndex = $bIndex = 0;
foreach($inv['A'] as $singles){
$bTemp[] = $singles;
$setsRecord[$singles][] = $aIndex;
for($i=$bIndex; $i < ($bMarker*2); ++$i) {
//echo $bIndex.' - '.($bMarker*2).'<br/>';
if(empty($inv['B'][$i])) {
$bTemp[] = 'null';
} else {
$bTemp[] = $inv['B'][$i];
$setsRecord[$inv['B'][$i]][] = $aIndex;
}
}
$createdSets[] = $bTemp;
$bTemp = array();
++$bMarker;
++$aIndex;
$bIndex = $bIndex + 2;
}
echo '<pre>';
print_r($createdSets);
print_r($setsRecord);
echo '</pre>';
To turn your array into an associative array, something like this can be done
<?php
$inventory=array(
array("A","PINK"),
array("A","MAUVE"),
array("A","ORANGE"),
array("A","GREY"),
array("B","RED"),
array("B","BLUE"),
array("B","YELLOW"),
array("B","GREEN"),
array("B","BLACK")
);
$inv = array();
foreach($inventory as $item){
$inv[$item[0]][] = $item[1];
}
echo '<pre>';
print_r($inv);
echo '</pre>';
Maybe you can use this function, assuming that:
... $inventory is already sorted (all A come before B)
... $inventory is a numeric array staring at index zero
// $set is the collection to which the generated sets are appended
// $inventory is your inventory, see the assumptions above
// $aCount - the number of A elements in a set
// $bCount - the number of B elements in a set
function makeSets(array &$sets, array $inventory, $aCount, $bCount) {
// extract $aItems from $inventory and shorten $inventory by $aCount
$aItems = array_splice($inventory, 0, $aCount);
$bItems = array();
// iterate over $inventory until a B item is found
foreach($inventory as $index => $item) {
if($item[0] == 'B') {
// extract $bItems from $inventory and shorten $inventory by $bCount
// break out of foreach loop after that
$bItems = array_splice($inventory, $index, $bCount);
break;
}
}
// append $aItems and $bItems to $sets, padd this array with null if
// less then $aCount + $bCount added
$sets[] = array_pad(array_merge($aItems, $bItems), $aCount + $bCount, null);
// if there are still values left in $inventory, call 'makeSets' again
if(count($inventory) > 0) makeSets($sets, $inventory, $aCount, $bCount);
}
$sets = array();
makeSets($sets, $inventory, 1, 2);
print_r($sets);
Since you mentioned that you dont have that much experience with arrays, here are the links to the php documentation for the functions I used in the above code:
array_splice — Remove a portion of the array and replace it with something else
array_merge — Merge one or more arrays
array_pad — Pad array to the specified length with a value
This code sorts inventory without any assumption on inventory ordering. You can specify pattern (in $aPattern), and order is obeyed. It also fills lacking entries with given default value.
<?php
# config
$aInventory=array(
array("A","PINK"),
array("A","MAUVE"),
array("A","ORANGE"),
array("A","GREY"),
array("B","RED"),
array("B","BLUE"),
array("B","YELLOW"),
array("B","GREEN"),
array("B","BLACK"),
array("C","cRED"),
array("C","cBLUE"),
array("C","cYELLOW"),
array("C","cGREEN"),
array("C","cBLACK")
);
$aPattern = array('A','B','A','C');
$mDefault = null;
# preparation
$aCounter = array_count_values($aPattern);
$aCurrentCounter = $aCurrentIndex = array_fill_keys(array_unique($aPattern),0);
$aPositions = array();
$aFill = array();
foreach ($aPattern as $nPosition=>$sElement){
$aPositions[$sElement] = array_keys($aPattern, $sElement);
$aFill[$sElement] = array_fill_keys($aPositions[$sElement], $mDefault);
} // foreach
$nTotalLine = count ($aPattern);
$aResult = array();
# main loop
foreach ($aInventory as $aItem){
$sElement = $aItem[0];
$nNeed = $aCounter[$sElement];
$nHas = $aCurrentCounter[$sElement];
if ($nHas == $nNeed){
$aCurrentIndex[$sElement]++;
$aCurrentCounter[$sElement] = 1;
} else {
$aCurrentCounter[$sElement]++;
} // if
$nCurrentIndex = $aCurrentIndex[$sElement];
if (!isset($aResult[$nCurrentIndex])){
$aResult[$nCurrentIndex] = array();
} // if
$nCurrentPosition = $aPositions[$sElement][$aCurrentCounter[$sElement]-1];
$aResult[$nCurrentIndex][$nCurrentPosition] = $aItem;
} // foreach
foreach ($aResult as &$aLine){
if (count($aLine)<$nTotalLine){
foreach ($aPositions as $sElement=>$aElementPositions){
$nCurrentElements = count(array_keys($aLine,$sElement));
if ($aCounter[$sElement] != $nCurrentElements){
$aLine = $aLine + $aFill[$sElement];
} // if
} // foreach
} // if
ksort($aLine);
# add empty items here
} // foreach
# output
var_dump($aResult);
Generic solution that requires you to specify a pattern of the form
$pattern = array('A','B','B');
The output will be in
$result = array();
The code :
// Convert to associative array
$inv = array();
foreach($inventory as $item)
$inv[$item[0]][] = $item[1];
// Position counters : int -> int
$count = array_fill(0, count($pattern),0);
$out = 0; // Number of counters that are "out" == "too far"
// Progression
while($out < count($count))
{
$elem = array();
// Select and increment corresponding counter
foreach($pattern as $i => $pat)
{
$elem[] = $inv[ $pat ][ $count[$i]++ ];
if($count[$i] == count($inv[$pat]))
$out++;
}
$result[] = $elem;
}