Trying to replace part of a file name with str_replace - php

I am creating a JQuery cycle for weather radar and warning images. I have the radar overlay functioning just fine. The directory containing the warning files contains 2 versions of each file but one has a similiar file names as the radar images. So I decided to use a str_replace to replace the one part of the filename from the radar images to the warning images. For some reason the str_replace is not working and I cannot for the life of me figure out why.
Thank you in advance for any help.
// Used to get radar images
echo '<div class="wp-forecast-img-cur">'."\n";
$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://radar.weather.gov/ridge/RadarImg/N0R/BOX/'), $matches);
foreach($matches[2] as $k => $match) {
if ($k < 1) continue;
echo '<img src="http://radar.weather.gov/ridge/RadarImg/N0R/BOX/' . $match . '" />'."\n";
}
echo '</div>'."\n";
// Used to get warning images
echo '<div class="wp-forecast-img-warn">'."\n";
$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://radar.weather.gov/ridge/RadarImg/N0R/BOX/'), $matches);
foreach($matches[2] as $k => $match) {
if ($k < 1) continue;
echo '<img src="http://radar.weather.gov/ridge/Warnings/Short/BOX/' . str_replace("NOR", "Warnings", $match) . '" />'."\n";
}
echo '</div>'."\n";
This line is just echoing the original file name and not replacing NOR with Warnings.
echo '<img src="http://radar.weather.gov/ridge/Warnings/Short/BOX/' . str_replace("NOR", "Warnings", $match) . '" />'."\n";
This is the get_text function in case it's needed.
function get_text($filename)
{
$fp_load = fopen("$filename", "rb");
if ( $fp_load )
{
while ( !feof($fp_load) )
{
$content .= fgets($fp_load, 8192);
}
fclose($fp_load);
return $content;
}
}
Variable Dump
array(32) {
[0]=> string(20) "/ridge/RadarImg/N0R/"
[1]=> string(25) "BOX_20130703_2220_N0R.gif"
[2]=> string(25) "BOX_20130703_2232_N0R.gif"
[3]=> string(25) "BOX_20130703_2238_N0R.gif"
[4]=> string(25) "BOX_20130703_2243_N0R.gif"
[5]=> string(25) "BOX_20130703_2249_N0R.gif"
[6]=> string(25) "BOX_20130703_2301_N0R.gif"
[7]=> string(25) "BOX_20130703_2306_N0R.gif"
[8]=> string(25) "BOX_20130703_2318_N0R.gif"
[9]=> string(25) "BOX_20130703_2324_N0R.gif"
[10]=> string(25) "BOX_20130703_2330_N0R.gif"
[11]=> string(25) "BOX_20130703_2335_N0R.gif"
[12]=> string(25) "BOX_20130703_2347_N0R.gif"
[13]=> string(25) "BOX_20130703_2353_N0R.gif"
[14]=> string(25) "BOX_20130703_2359_N0R.gif"
[15]=> string(25) "BOX_20130704_0004_N0R.gif"
[16]=> string(25) "BOX_20130704_0016_N0R.gif"
[17]=> string(25) "BOX_20130704_0022_N0R.gif"
[18]=> string(25) "BOX_20130704_0027_N0R.gif"
[19]=> string(25) "BOX_20130704_0033_N0R.gif"
[20]=> string(25) "BOX_20130704_0045_N0R.gif"
[21]=> string(25) "BOX_20130704_0056_N0R.gif"
[22]=> string(25) "BOX_20130704_0108_N0R.gif"
[23]=> string(25) "BOX_20130704_0114_N0R.gif"
[24]=> string(25) "BOX_20130704_0125_N0R.gif"
[25]=> string(25) "BOX_20130704_0137_N0R.gif"
[26]=> string(25) "BOX_20130704_0148_N0R.gif"
[27]=> string(25) "BOX_20130704_0154_N0R.gif"
[28]=> string(25) "BOX_20130704_0206_N0R.gif"
[29]=> string(25) "BOX_20130704_0211_N0R.gif"
[30]=> string(25) "BOX_20130704_0223_N0R.gif"
[31]=> string(25) "BOX_20130704_0229_N0R.gif"
}

I think that the str_replace does not work because the string you need to replace is not "NOR" but "N0R" (N-zero-R).

Related

How to get the keys from preg_match_all?

The code was working:
$url = 'http://www.google.com/search?hl=en&tbo=d&site=&source=hp&q=upoznavanje';
$html = file_get_html($url);
preg_match_all('/(?<="><cite>).*?(?=<\/cite><div\ class=)/', $html, $output);
foreach ($output[0] as $link) {
$link ."<br>" ;
}
When I added echo $output[0], now I get 0, and nothing in $output[1].
var_dump works and print_r array is there, but how do I get unique values of each without foreach?
var_dump:
array(1) { [0]=> array(10) { [0]=> string(21) "https://www.elmaz.rs/" [1]=> string(47) "https://badoo.com/sr/upoznavanje/serbia/" [2]=> string(39) "https://serbiandating.com/?locale=sr_RS" [3]=> string(30) "https://www.lepoticaizver.com/" [4]=> string(22) "www.prvi-sastanak.net/" [5]=> string(122) "https://www.telegraf.rs/.../1878507-top-5-aplikacija-za-muvanje-ovo-su- najbolji-sajtovi-za-upoznavanje-i-dejt-foto" [6]=> string(137) "https://www.telegraf.rs/.../1863749-dopisivala-sam-se-sa-muskarcinama-na- sajtu-za-upoznavanje-uh-kakve-sam-sve-ponude-dobila-foto" [7]=> string(24) "https://www.iskrica.com/"
I found how to do that with extracting single data from array without loop:
print_r($output[0][0]);

Data read and output with a loop

I make an event list with a maximum of 4 events, saved in its own jason files.
I wrote this code for the output.
JSON:
{"field1":"new york","field2":"1553404200","field3":1554415200}
CODE:
$jsonA = file_get_contents('code/data1.json');
$fieldsA = json_decode($jsonA, true);
$cityA = $fieldsA["field1"];
$dateStartA = $fieldsA["field2"];
$dateEndA = $fieldsA["field3"];
$jsonB = file_get_contents('code/data2.json');
$fieldsB = json_decode($jsonB, true);
$cityB = $fieldsB["field1"];
$dateStartB = $fieldsB["field2"];
$dateEndB = $fieldsB["field3"];
<div><?php echo $cityA . $dateStartA . " - " . $dateEndA?></div>
<div><?php echo $cityB . $dateStartB . " - " . $dateEndB?></div>
The problem is:
I have 4 events with each start and end date. So I have to do the same 8 times, but I do not want and should copy this code 8 times with a higher variable name...
In my project I have 13 fields and also 2 arrays to output the day and month name in my language. That's why it's so important not to copy everything 8 times.
I need a loop, but I do not know exactly how. I'm an absolute beginner, so please as simple as possible.
Great job with your attempt!
You might just want to write a foreach and read your files and store it in an array.
In your local server, you might just want to find the absolute path to your code folder. If you open your terminal, and CTRL+C code directory and CTRL+V in your terminal, you might see what your absolute path may look like. Then, paste upto before code directory in $server_path. Then, run the code, hopefully it would work.
function searchFilenames($array, $re)
{
return preg_grep('/' . preg_quote($re, '/') . '/i', $array);
}
$server_path = '/path/to/your/localserver/';
$dir = $server_path . 'code';
$filename_array = searchFilenames(glob($dir . "/*"), 'data');
$fields = array();
foreach ($filename_array as $key => $filename) {
$jsonA = file_get_contents($filename);
$values = json_decode($jsonA, true);
$fields[$key]["city"] = $values["field1"];
$fields[$key]["dateStart"] = $values["field2"];
$fields[$key]["dateEnd"] = $values["field3"];
}
var_dump($fields);
Output
array(8) {
[0]=>
array(3) {
["city"]=>
string(8) "new york"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[1]=>
array(3) {
["city"]=>
string(6) "boston"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[2]=>
array(3) {
["city"]=>
string(7) "chicago"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[3]=>
array(3) {
["city"]=>
string(13) "washington dc"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[4]=>
array(3) {
["city"]=>
string(5) "miami"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[5]=>
array(3) {
["city"]=>
string(10) "los angles"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[6]=>
array(3) {
["city"]=>
string(7) "seattle"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[7]=>
array(3) {
["city"]=>
string(6) "austin"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
}

Merge Two csv files with php

i have two csv files: one contracts data and the other containing awarded contracts. I need to combine these two files using common field(contractname) and compute total amount of current awarded contracts.
Link to csv files:
https://github.com/younginnovations/problem-statements/tree/master/clean-up-contracts-data
I do get desired output bt undefined offset error comes up.
the code:unset(awards_info[$y][0]); does unsets the data but 'Undefined offset: 0' error occurs..
similarly the code $list_data[10]!=NULL also shows the same error
but both are producing desired output.
var_dump($awards_info):: produces following:
array(5) { [0]=> array(6) { [0]=> string(12) "contractName" [1]=> string(12) "contractDate" [2]=> string(14) "completionDate" [3]=> string(7) "awardee" [4]=> string(15) "awardeeLocation" [5]=> string(6) "Amount" } [1]=> array(6) { [0]=> string(15) "Contract-2070-3" [1]=> string(8) "5/9/2014" [2]=> string(9) "8/25/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "200000" } [2]=> array(6) { [0]=> string(15) "Contract-2070-5" [1]=> string(9) "3/18/2014" [2]=> string(8) "4/8/2014" [3]=> string(24) "S engineering industries" [4]=> string(9) "Makwanpur" [5]=> string(6) "300000" } [3]=> array(6) { [0]=> string(15) "Contract-2070-9" [1]=> string(8) "3/6/2014" [2]=> string(8) "4/6/2014" [3]=> string(24) "Gourishankar nirman sewa" [4]=> string(8) "Lalitpur" [5]=> string(6) "400000" } [4]=> array(6) { [0]=> string(16) "Contract-2070-10" [1]=> string(8) "2/6/2014" [2]=> string(9) "6/16/2014" [3]=> string(11) "SK Builders" [4]=> string(5) "Banke" [5]=> string(6) "400000" } }
$file_contract=fopen('contracts.csv','r');
$file_awards=fopen('awards.csv','r');
while(($data=fgetcsv($file_contract))!=FALSE){
$contracts_info[]=$data;
}
while(($data=fgetcsv($file_awards))!=FALSE){
$awards_info[]=$data;
}
$con_count=count($contracts_info);
for($x=0;$x<$con_count;$x++){
if($x==0){
unset($awards_info[0][0]);
$data[$x]=array_merge($contracts_info[0],$awards_info[0]);
}
else{
$check=0;
$award_count=count($awards_info);
for($y=1;$y<$award_count;$y++){
if($awards_info[$y][0]==$contracts_info[$x][0]){
unset($awards_info[$y][0]);
$data[$x]=array_merge($contracts_info[$x],$awards_info[$y]);
$check=1;
}
}
if($check==0){
$data[$x]=$contracts_info[$x];
}
}
}
$final_data=fopen('final.csv','w');
foreach($data as $list_data){
fputcsv($final_data,$list_data);
}
fclose($final_data);
$c=0;
foreach ($data as $list_data){
if(($list_data[1]=='Current') && ($list_data[10]!=NULL))
{
$c+=$list_data[12];
}
}
echo $c;
You can just add the content file in the new file, without parsing files :
$fp = fopen('final.csv','w');
fwrite($fp, file_get_contents('contracts.csv'));
fwrite($fp, file_get_contents('awards.csv'));
fclose($fp);
echo file_get_contents('final.csv');
ok it's a wrong way. So i try to rewrite the loop :
for($x=0; $x < count($contracts_info); $x++) {
$data[$x] = $contracts_info[$x];
if($x == 0) {
if(isset($awards_info[$x])) {
$data[$x] = array_merge($data[$x], $awards_info[$x]);
unset($awards_info[$x]);
}
} else {
foreach($awards_info as $y => $award_info_datas) {
if($award_info_datas[0] == $contracts_info[$x][0]) {
$data[$x] = array_merge($data[$x], $award_info_datas);
unset($awards_info[$y]);
break;
}
}
}
}

Access multidimensional array in php?

array(10) {
["table_name"]=>
string(0) ""
["chart_type"]=>
string(5) "Table"
["chart_name"]=>
string(9) "Veterans "
["dashboard_name"]=>
string(7) "Default"
["data_option"]=>
string(0) ""
["limit_value"]=>
string(1) "0"
["view_status"]=>
bool(true)
["view_priority"]=>
string(0) ""
["chart_data"]=>
array(72) {
[0]=>
array(11) {
[0]=>
string(4) "Rank"
[1]=>
string(6) "Branch"
[2]=>
string(12) "Level 3 Name"
[3]=>
string(12) "Level 4 Name"
[4]=>
string(23) "Logged Premia Score MTD"
[5]=>
string(26) "Converted Premia Score MTD"
[6]=>
string(21) "Persistancy Score MTD"
[7]=>
string(23) "Logged Premia Score YTD"
[8]=>
string(26) "Converted Premia Score YTD"
[9]=>
string(21) "Persistancy Score YTD"
[10]=>
string(5) "Score"
}
[1]=>
array(11) {
[0]=>
float(1)
[1]=>
string(22) " GWALIOR DIRECTSegment"
[2]=>
string(29) "G00563 - GOVIND SINGH KUSHWAH"
[3]=>
string(21) "S03643 - Shishir Jain"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.9994)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.89901)
}
[2]=>
array(11) {
[0]=>
float(2)
[1]=>
string(26) " KURUKSHETRA DIRECTSegment"
[2]=>
string(23) "A02311 - Amarjeet Singh"
[3]=>
string(21) "S00927 - Sachin Kumar"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.953)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.82245)
}
}
["color_scheme_name"]=>
string(7) "Default"
}
How do i display the chart_data in php
$arr=json_decode($row[0],TRUE);
var_dump($arr);
foreach($arr as $row)
{
foreach($row['chart_data'] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
EDIT:
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo $val;
}
}
You are having 2 foreach calls.
The first one passes all keys of the outer array, amongst which chart_data.
The second one looks at the children of the outer array, and looks for chart_data in each child. But is obviously not finding any.
Change your code to this:
$arr=json_decode($row[0],TRUE);
foreach($arr['chart_data'] as $k){
echo $k['Rank'];
echo $k['Branch'];
}
edit:
I see. (Thanks for indenting your array) Your problem is off course that $arr['rank']and $arr['branch'] don't exist. They are mere strings inside $k[0] and $k[1] and themselves don't contain any other data.
What would you have like to see outputted?
Maybe this is solution.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
Update :
Rank, Branch is values. Your array is bad format.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k[0];
echo $k[1];
}
}

preg_match , pattern, php

I'm trying to extract some specific pictures from html content . Currently I have the following array
[1] => Array
(
[0] => BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_14.JPG#!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG|1#http://i.ebayimg.com/08#!BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~_14.JPG#!BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~_12.JPG|2#http://i.ebayimg.com/13#!BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~_14.JPG#!BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~_12.JPG|3#http://i.ebayimg.com/03#!BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~_14.JPG#!BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~_12.JPG|4#http://i.ebayimg.com/09#!BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~_14.JPG#!BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~_12.JPG|5#http://i.ebayimg.com/17#!BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~_14.JPG#!BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~_12.JPG|6#http://i.ebayimg.com/17#!BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~_14.JPG#!BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~_12.JPG|7#http://i.ebayimg.com/12#!BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~_14.JPG#!BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~_12.JPG|8#http://i.ebayimg.com/20#!BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~_14.JPG#!BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~_12.JPG|9#http://i.ebayimg.com/24#!BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_14.JPG#!BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_12.JPG|10#http://i.ebayimg.com/16#!BuE,IOgBmk~$(KGrHqEH-EMEvpym7mSLBL-vdqI6nw~~_14.JPG#!BuE,IOgBmk~$(KGrHqEH-EMEvpym7mSLBL-vdqI6nw~~
)
I need to extract only the pictures that start with ! and ends in 12.JPG . An example of expected result is
!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG
. I have a pattern but it doesn't work for some reasons that I don't know . It is
$pattern = "/#\!(.*)_12.JPG/";
Thank you in advance for any help
Not a regex problem.
$names = explode('#', $names);
$keep = array();
foreach ($names as $name)
{
if ((substr($name, 0, 1) == '!') && (substr($name, -6) == '12.JPG'))
{
$keep[] = $name;
}
}
Try using '/#\!([^_]+)_12.JPG/' instead. Also, you're using preg_match_all, right?
Example ..
<?php
$input = '(that long string you have)';
$matches = array();
preg_match_all('/#\!([^_]+)_12.JPG/', $input, $matches);
var_dump($matches);
Output (notice how $matches[0] contains all full matches, and $matches[1] contains all grouped (up to _12.JPG) matches) ..
array(2) {
[0]=>
array(10) {
[0]=>
string(53) "#!BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~_12.JPG"
[1]=>
string(53) "#!BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~_12.JPG"
[2]=>
string(53) "#!BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~_12.JPG"
[3]=>
string(53) "#!BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~_12.JPG"
[4]=>
string(53) "#!BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~_12.JPG"
[5]=>
string(53) "#!BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~_12.JPG"
[6]=>
string(53) "#!BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~_12.JPG"
[7]=>
string(53) "#!BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~_12.JPG"
[8]=>
string(53) "#!BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~_12.JPG"
[9]=>
string(53) "#!BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~_12.JPG"
}
[1]=>
array(10) {
[0]=>
string(44) "BuEZm5g!Wk~$(KGrHqQH-DgEvrb2CLuOBL-vbkHlKw~~"
[1]=>
string(44) "BuEZqMQBGk~$(KGrHqQH-DoEvrKYSoPiBL-vb)WgLw~~"
[2]=>
string(44) "BuEZtkw!2k~$(KGrHqYH-EYEvsh4EjJSBL-vb-bLow~~"
[3]=>
string(44) "BuEZ)!wEGk~$(KGrHqYH-DwEvq8Z1JuQBL-vcMOoFQ~~"
[4]=>
string(44) "BuEZ1IQEGk~$(KGrHqEH-D0EvqwClvviBL-vcclJwg~~"
[5]=>
string(44) "BuEZ4FQCWk~$(KGrHqUH-DMEvS+,FRj5BL-vco)Qgg~~"
[6]=>
string(44) "BuEZ7FQEWk~$(KGrHqYH-EYEvsh4EjJSBL-vc1v2Hg~~"
[7]=>
string(44) "BuEZ-c!Bmk~$(KGrHqQH-CYEvr5z9)NVBL-vdC,)Mg~~"
[8]=>
string(44) "BuE,CNgCWk~$(KGrHqIH-CIEvqKBurmhBL-vdRBe3!~~"
[9]=>
string(44) "BuE,FN!EWk~$(KGrHqUH-C0EvsBdjbv0BL-vdeFkD!~~"
}
}

Categories