I have two arrays: $ORGvarvals (ORGvarvals contains the values which needs to be replaced in String) & $varvals (varvals contains the values which needs to be replaced with ORGvarvals in string)
$str (Contains the content).
This is my $ORGvarval:
Array ( [0] => 56,231 [1] => abc [2] => 172.0.0.1 [3] => JMeter [4] => http://172.0.0.1/file/path/ [5] => ${__time(yyyy/MM/dd HH:mm:ss,)} [6] => API [7] => Unique [8] => /home/path/to/file/filename_${__time(yyyy-MM-dd_HH-mm-ss,)}.xls [9] => C:\xls\file\path\results${__time(ddMMyyyyHHmmss,)}.csv [10] => 172.0.0.1 [11] => 9999 [12] => 22 [13] => username [14] => password [15] => /tomcat/path [16] => C:\path\to\testdata [17] => jdbc:oracle:thin:db:details [18] => username1 [19] => password1 [20] => /USSD/API/URL?parameter [21] => /EXTGW/API/URL?parameter [22] => /USSD/API/URL?parameter2 [23] => /EXTGW/API/URL?parameter [24] => /EXTGW/API/URL?parameter5 [25] => /USSD/API/URL?parameter6 [26] => 310 [27] => 1234567890 [28] => 2468 [29] => 1357 [30] => 9876543210 [31] => 100 [32] => CC [33] => 500 [34] => 50 [35] => 100 [36] => 100 [37] => /path/to/logs [38] => 1237896540 [39] => 1357 [40] => abc#7891 [41] => 0 [42] => 0 [43] => 101 [44] => dd/MM/yy [45] => ADMIN [46] => abc#7891 [47] => 1357 [48] => 3 [49] => 10 [50] => jmeter_logs )
I am taking user inputs through form and all the values are saved in another variable $varvals in same format.
Now I want to find the $ORGvarvals values one by one in $str and replace them with $varvals.
I tried the following two codes:
$str=str_replace($ORGvarvals, $varvals, $str);
$str=strtr($str, array_combine($ORGvarvals, $varvals));
Both of them are not working as expected. They are replacing multiple occurrences in string.
I want to find all the ORGvarvals values one by one and replace them without messing up the already replaced values in case of duplicate values
Related
Parsing a CSV file for getting an array some lines are not parsed correctly with fgetcsv; tried with "100", "0" and "1000" line length while the csv file lines were smaller than 80 chars; The separator char was ";":
fgetcsv($h, 0, ";");
I did the same with explode and everything works fine:
$line = fgets($handle);
$pieces = explode(";", $line);
I will use the latest, but, still, I would like to understand what was wrong with fgetcsv. Any idea?
Here more details:
CODE
$h = fopen(FCPATH . $file_dir, "r");
$handle = fopen(FCPATH . $file_dir, "r");
$header = fgetcsv($h, 0, ";");
$line = fgets($handle);
while (($data = fgetcsv($h, 1000, ";")) !== FALSE) {
$line = fgets($handle);
log_message('error', "\n-----------START-------\n\t". print_r($line,1));
log_message('error', "\n\t". print_r($data,1));
$pieces = explode(";", $line);
log_message('error', "\n\t". print_r($pieces,1)."\n-----------END---------\n\n");
//... other code
}
LOG OUTPUT
many lines parsed correctly like this
RIGHE DATI
ERROR - 2019-02-16 19:21:47 -->
-----------START-------
2538;;;Emergency Contraception;http://track.healthtrader.com/track.php?c=cmlkPTcxMjMyOSZhaWQ9NTQ1NDI0MjU;;;;ellaOne;http://track.healthtrader.com/track.php?c=cmlkPTcxMjMzMCZhaWQ9NTQ1NDI0MjU;pills;;;;;http://www.euroclinix.net/en/images/product/ellaone-s.jpg;;http://www.euroclinix.net/en/images/product/ellaone-m.jpg;Emergency Contraceptive: ellaOne Pill;http://www.euroclinix.net/en/images/product/ellaone-l.jpg;;;Y;0;GBP;œ;8995;;http://track.healthtrader.com/track.php?c=cmlkPTcxNjExNiZhaWQ9NTQ1NDI0MjU;Emergency Contraception<br>;;ellaOne 30mg x 2;http://www.euroclinix.net/en/images/product/hra-pharma-logo.jpg;HRA Pharma;http://www.euroclinix.net/en/images/product/ellaone-t.jpg;
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 2538
[1] =>
[2] =>
[3] => Emergency Contraception
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMyOSZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => ellaOne
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMzMCZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] =>
[12] =>
[13] =>
[14] =>
[15] => http://www.euroclinix.net/en/images/product/ellaone-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/ellaone-m.jpg
[18] => Emergency Contraceptive: ellaOne Pill
[19] => http://www.euroclinix.net/en/images/product/ellaone-l.jpg
[20] =>
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 8995
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjExNiZhaWQ9NTQ1NDI0MjU
[29] => Emergency Contraception<br>
[30] =>
[31] => ellaOne 30mg x 2
[32] => http://www.euroclinix.net/en/images/product/hra-pharma-logo.jpg
[33] => HRA Pharma
[34] => http://www.euroclinix.net/en/images/product/ellaone-t.jpg
[35] =>
)
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 2538
[1] =>
[2] =>
[3] => Emergency Contraception
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMyOSZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => ellaOne
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjMzMCZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] =>
[12] =>
[13] =>
[14] =>
[15] => http://www.euroclinix.net/en/images/product/ellaone-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/ellaone-m.jpg
[18] => Emergency Contraceptive: ellaOne Pill
[19] => http://www.euroclinix.net/en/images/product/ellaone-l.jpg
[20] =>
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 8995
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjExNiZhaWQ9NTQ1NDI0MjU
[29] => Emergency Contraception<br>
[30] =>
[31] => ellaOne 30mg x 2
[32] => http://www.euroclinix.net/en/images/product/hra-pharma-logo.jpg
[33] => HRA Pharma
[34] => http://www.euroclinix.net/en/images/product/ellaone-t.jpg
[35] =>
)
-----------END---------
Some lines not parsed correctly by fgetcsv, but still parsed correctly by explode, as in this case
ERROR - 2019-02-16 19:21:47 -->
-----------START-------
7667;;;Diabetes;http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MCZhaWQ9NTQ1NDI0MjU;;;;Metformin;http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MyZhaWQ9NTQ1NDI0MjU;pills;Teva;;;;http://www.euroclinix.net/en/images/product/metformin-pack-s.jpg;;http://www.euroclinix.net/en/images/product/metformin-pack-m.jpg;;http://www.euroclinix.net/en/images/product/metformin-l.jpg;Large view of sealed box pack that contains Metformin 500mg tablets. ;;Y;0;GBP;œ;2995;;http://track.healthtrader.com/track.php?c=cmlkPTcxNjQ1MCZhaWQ9NTQ1NDI0MjU;Diabetes<br>;;Metformin 500mg x 168;http://www.euroclinix.net/en/images/product/pfizer_logo.gif;Pfizer Pharmaceutical;http://www.euroclinix.net/en/images/product/metformin-pack-t.jpg;
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 1686
[1] =>
[2] =>
[3] => Erectile Dysfunction
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1NCZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => Cialis
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1NSZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] => Lilly
[12] =>
[13] => Cialis (tadalafil) is a fast acting prescription treatment for men suffering with erectile problems
[14] => Cialis (tadalafil) is a prescription impotence treatment that helps men with erectile problems. Cialis helps men to get an erection and sustain an erection, working in a little as 30 minutes. Cialis is the longest lasting impotence medication, with benefits experienced for up to 36 hours.
[15] => http://www.euroclinix.net/en/images/product/cialis-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/cialis-m.jpg
[18] =>
[19] => http://www.euroclinix.net/en/images/product/cialis-l.jpg
[20] => Cialis medication is sealed inside a box for better protection.
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 18495
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjM3NiZhaWQ9NTQ1NDI0MjU
[29] => Impotence<br>
[30] =>
[31] => Cialis 10mg x 12
[32] => http://www.euroclinix.net/en/images/product/lilly-
)
ERROR - 2019-02-16 19:21:47 -->
Array
(
[0] => 7667
[1] =>
[2] =>
[3] => Diabetes
[4] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MCZhaWQ9NTQ1NDI0MjU
[5] =>
[6] =>
[7] =>
[8] => Metformin
[9] => http://track.healthtrader.com/track.php?c=cmlkPTcxMjI1MyZhaWQ9NTQ1NDI0MjU
[10] => pills
[11] => Teva
[12] =>
[13] =>
[14] =>
[15] => http://www.euroclinix.net/en/images/product/metformin-pack-s.jpg
[16] =>
[17] => http://www.euroclinix.net/en/images/product/metformin-pack-m.jpg
[18] =>
[19] => http://www.euroclinix.net/en/images/product/metformin-l.jpg
[20] => Large view of sealed box pack that contains Metformin 500mg tablets.
[21] =>
[22] => Y
[23] => 0
[24] => GBP
[25] => œ
[26] => 2995
[27] =>
[28] => http://track.healthtrader.com/track.php?c=cmlkPTcxNjQ1MCZhaWQ9NTQ1NDI0MjU
[29] => Diabetes<br>
[30] =>
[31] => Metformin 500mg x 168
[32] => http://www.euroclinix.net/en/images/product/pfizer_logo.gif
[33] => Pfizer Pharmaceutical
[34] => http://www.euroclinix.net/en/images/product/metformin-pack-t.jpg
[35] =>
)
-----------END---------
This question already has answers here:
How to extract html comments and all html contained by node?
(4 answers)
Closed 4 years ago.
Need a help. I want to get the commented array vai php regex or something like that and insert into database. Anyone have any idea how can i will get that commented array vai php ? I really appreciate that . Thanks in advance :)
<body>
<!--Array
(
[0] => Pagedale
[1] => 3,304.
[2] => 1.19
[3] => $28,480
[4] => 93.43%
[5] => 22.40%
[6] => 0.2640
[7] => 0.3410
[8] => 0.0000
[9] => 0.3500
[10] => $189,823
[11] => 6.83%
[12] => 8.363%
[13] => $1,090,378
[14] => 39.25%
[15] => 1
[16] => $2,434,084.00
[17] => $2,778,093
[18] => $2,540,416
[19] => Sales Tax
[20] => $1,090,378
[21] => Utility Tax
[22] => $471,471
[23] => Court Fines/Fees
[24] => $351,583
[25] => Parks & Recreation
[26] => $475,127
[27] => Police
[28] => $185,013
[29] => TIF Match
[30] => $60,153
[31] => $47,417
[32] => 7
[33] => Pagedale PD
[34] => 17
[35] => Pagedale
[36] => 12.66%
[37] => $90,758
[38] => $351,583
[39] => 1420 Ferguson Ave
[40] => Pagedale
[41] => MO
[42] => 63133
[43] => 314-726-1200
[44] => http://www.cityofpagedale.com
[45] => M-F (9-5)
[46] => The Board of Aldermen meet on the second Thursday of each month at 7:30pm
[47] => 1420 Ferguson Auditorium
[48] => 1
)
-->
<div>Hello world </div>
</body>
</html>
To locate comments within the HTML markup I would suggest that a regex is probably not the best method but you can accomplish the stated aim easily using DOMDocument and DOMXPath. There is, within XPath 2 a comment() function that you would use in the XPath query - viz: $xp->query('//node/comment()') etc
$strhtml='
<html>
<head>
<title>duff html</title>
</head>
<body>
<!--
first of too many
-->
<!--Array
(
[0] => Pagedale
[1] => 3,304.
[2] => 1.19
[3] => $28,480
[4] => 93.43%
[5] => 22.40%
[6] => 0.2640
[7] => 0.3410
[8] => 0.0000
[9] => 0.3500
[10] => $189,823
[11] => 6.83%
[12] => 8.363%
[13] => $1,090,378
[14] => 39.25%
[15] => 1
[16] => $2,434,084.00
[17] => $2,778,093
[18] => $2,540,416
[19] => Sales Tax
[20] => $1,090,378
[21] => Utility Tax
[22] => $471,471
[23] => Court Fines/Fees
[24] => $351,583
[25] => Parks & Recreation
[26] => $475,127
[27] => Police
[28] => $185,013
[29] => TIF Match
[30] => $60,153
[31] => $47,417
[32] => 7
[33] => Pagedale PD
[34] => 17
[35] => Pagedale
[36] => 12.66%
[37] => $90,758
[38] => $351,583
[39] => 1420 Ferguson Ave
[40] => Pagedale
[41] => MO
[42] => 63133
[43] => 314-726-1200
[44] => http://www.cityofpagedale.com
[45] => M-F (9-5)
[46] => The Board of Aldermen meet on the second Thursday of each month at 7:30pm
[47] => 1420 Ferguson Auditorium
[48] => 1
)
-->
<div>Hello world </div>
<!-- last comment -->
<div> Last div </div>
</body>
</html>';
$dom=new DOMDocument;
$dom->loadHTML( $strhtml );
$xp=new DOMXPath( $dom );
$col=$xp->query('/html/body/comment()');
if( $col && $col->length > 0 ){
$comments=[];
foreach( $col as $comment ){
$comments[]=$comment->nodeValue;
}
printf('<pre>%s</pre>',print_r($comments,true));
}
Will output:
Array
(
[0] =>
first of too many
[1] => Array
(
[0] => Pagedale
[1] => 3,304.
[2] => 1.19
[3] => $28,480
[4] => 93.43%
[5] => 22.40%
[6] => 0.2640
[7] => 0.3410
[8] => 0.0000
[9] => 0.3500
[10] => $189,823
[11] => 6.83%
[12] => 8.363%
[13] => $1,090,378
[14] => 39.25%
[15] => 1
[16] => $2,434,084.00
[17] => $2,778,093
[18] => $2,540,416
[19] => Sales Tax
[20] => $1,090,378
[21] => Utility Tax
[22] => $471,471
[23] => Court Fines/Fees
[24] => $351,583
[25] => Parks & Recreation
[26] => $475,127
[27] => Police
[28] => $185,013
[29] => TIF Match
[30] => $60,153
[31] => $47,417
[32] => 7
[33] => Pagedale PD
[34] => 17
[35] => Pagedale
[36] => 12.66%
[37] => $90,758
[38] => $351,583
[39] => 1420 Ferguson Ave
[40] => Pagedale
[41] => MO
[42] => 63133
[43] => 314-726-1200
[44] => http://www.cityofpagedale.com
[45] => M-F (9-5)
[46] => The Board of Aldermen meet on the second Thursday of each month at 7:30pm
[47] => 1420 Ferguson Auditorium
[48] => 1
)
[2] => last comment
)
I have a query which fetches one table and then the results of that query gets passed though another query.
Then I want to return all the values of that column in each row from that query.
This fetches all of the Product id's with a particular brand ID
// Fetch the Product List
$brandID = 1;
$prodList = Products::whereBrandId($brandID);
// Fetch the catalog ID of the Product
$fetchID = $prodList->lists('id');
Then print_r($fetchID) returns the array.
Array ( [0] => 10011 [1] => 10012 [2] => 10013 [3] => 10014 [4] => 10015 [5] => 10016 [6] => 10017 [7] => 10018 [8] => 10019 [9] => 10020 [10] => 10021 [11] => 10022 [12] => 10023 [13] => 10024 [14] => 10025 [15] => 10026 [16] => 10027 [17] => 10028 [18] => 10029 [19] => 10030 [20] => 10031 [21] => 10032 [22] => 10033 [23] => 10034 [24] => 10035 [25] => 10036 [26] => 10037 [27] => 10038 [28] => 10039 [29] => 10040 [30] => 10041 [31] => 10042 [32] => 10043 [33] => 10044 [34] => 10045 [35] => 10046 [36] => 10047 [37] => 10048 [38] => 10049 [39] => 10050 [40] => 10051 [41] => 10052 [42] => 10053 [43] => 10054 [44] => 10055 [45] => 10056 [46] => 10057 [47] => 10058 [48] => 10059 [49] => 10060 [50] => 10061 [51] => 10062 [52] => 10063 [53] => 10064 [54] => 10065 [55] => 10066 [56] => 10067 [57] => 10068 [58] => 10069 [59] => 10070 [60] => 10071 [61] => 10072 [62] => 10073 [63] => 10074 [64] => 10075 [65] => 10076 [66] => 10077 [67] => 10078 [68] => 10079 [69] => 10080 [70] => 10092 [71] => 10093 [72] => 10128 )
I then have a table with the fields product_id and category_id, so I want to pass the results of $fetchID though and return all values from the category_id using lists()
// Fetch the category_id where is a product_id
$catRelation = Db::table('purple_catalog_prods_cats')->whereProductId($fetchID);
$catRelList = $catRelation->lists('category_id');
This returns as empty in print_r
Finally I want to query the categories table which has id and name, and return everything. So I try to pass the $catRelList through. This doesn't work because in the previous query it returns as empty.
// Fetch the Cat list
$catList = categoryName::whereId($catRelList)->orderBy('id', 'asc');
$this->categoryName = $catList->get();
So my issue is passing though $fetchID to return all rows matching the multiple Product ID's. As when I manually enter a product ID it returns that category fine. Query Below
$catRelation = Db::table('purple_catalog_prods_cats')->whereProductId('10011');
Now where the 10011 is, I somehow want to pass multiple values through, like in the $fetchID array.
Is this possible to do and is there a better method of doing this?
Change your $catRelation query to using whereIn:
$catRelation = Db::table('purple_catalog_prods_cats')->whereIn('product_id', $fetchID);
This should now correctly using your array of ProductIDs and find all matching rows. (Change column name as required).
I have a single dimentional PHP Array that has latitude, longitude, and time data. The data goes eg [lat, long, date, lat, long, date, lat, long, date.... etc etc]
Array ( [0] => -28.0447606 [1] => 153.4340961 [2] => 1424136836118 [3] => -28.0447612 [4] => 153.4340963 [5] => 1424136876189 [6] => -28.0447658 [7] => 153.4340962 [8] => 1424136897993 [9] => -28.0447619 [10] => 153.4340615 [11] => 1424136918045 [12] => -28.0447656 [13] => 153.434057 [14] => 1424136938057 [15] => -28.0447613 [16] => 153.4340484 [17] => 1424136958085 [18] => -28.0447791 [19] => 153.4340959 [20] => 1424136978117 [21] => -28.0447584 [22] => 153.4340501 [23] => 1424136998135 [24] => -28.0447676 [25] => 153.434047 [26] => 1424137018179 [27] => -28.044782 [28] => 153.4340982 [29] => 1424137038185 [30] => -28.0447599 [31] => 153.4340496 [32] => 1424137058214 [33] => -28.0447614 [34] => 153.4340531 [35] => 1424137078589 [36] => -28.0447588 [37] => 153.4340963 [38] => 1424137098731 [39] => -28.0447768 [40] => 153.434098 [41] => 1424137138640 [42] => -28.0447141 [43] => 153.4341097 [44] => 1424137158672 [45] => -28.0447628 [46] => 153.4340962 [47] => 1424137178698 [48] => -28.0447622 [49] => 153.4340962 [50] => 1424137198726 [51] => -28.0447528 [52] => 153.4340936 [53] => 1424137218871 [54] => -28.0447636 [55] => 153.4340472 [56] => 1424137258825 [57] => -28.0447608 [58] => 153.434097 [59] => 1424137279945 [60] => -28.0447656 [61] => 153.4340979 [62] => 1424137300018 )
I am just wondering if there is an easy way to convert this into a two dimensional array so I can access them like $gps[0][1] or $gps[3][0] etc. I've tried a few ways like using a for loop, but surely there's some other way I'm overlooking.
If I'm not wrong this is what you want.
You just need to change the $arr for your variable.
$coords = array_map(function($coords) {
return array(
"lat" => $coords[0], # latitude
"long" => $coords[1], # longitude
"ts" => $coords[2] # timestamp
);
}, array_chunk($arr, 3));
/*
Array (
Array (
[lat] => 12
[long] => 34
[ts] => 56
)
Array (
[lat] => 12
[long] => 34
[ts] => 56
)
... and so on
)
*/
I want to filter some data using regex. As for now I have some text going over 2 lines and I tried to make the linebreak match with [^.*]. But it seems to not pass the newline and so it doesn't match any result on the second line. How can I include the linebreak? I tried something like [^\n\r.*] but it didn't worked out.
Description
You could use the 's' option which forces the dot to match all new line characters, or if you don't have control over the underlying code you could try:
([^.]|[.])
This will match every character. The dot will sometimes not match the carriage return, new line.
PHP example
<?php
$sourcestring="This is my.
super cool
test string";
preg_match_all('/([^.]|[.])/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
$matches Array:
(
[0] => Array
(
[0] => T
[1] => h
[2] => i
[3] => s
[4] =>
[5] => i
[6] => s
[7] =>
[8] => m
[9] => y
[10] => .
[11] =>
[12] =>
[13] => s
[14] => u
[15] => p
[16] => e
[17] => r
[18] =>
[19] => c
[20] => o
[21] => o
[22] => l
[23] =>
[24] =>
[25] => t
[26] => e
[27] => s
[28] => t
[29] =>
[30] => s
[31] => t
[32] => r
[33] => i
[34] => n
[35] => g
)
[1] => Array
(
[0] => T
[1] => h
[2] => i
[3] => s
[4] =>
[5] => i
[6] => s
[7] =>
[8] => m
[9] => y
[10] => .
[11] =>
[12] =>
[13] => s
[14] => u
[15] => p
[16] => e
[17] => r
[18] =>
[19] => c
[20] => o
[21] => o
[22] => l
[23] =>
[24] =>
[25] => t
[26] => e
[27] => s
[28] => t
[29] =>
[30] => s
[31] => t
[32] => r
[33] => i
[34] => n
[35] => g
)
)