Regex match every character and newline - php

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
)
)

Related

PHP multi dimensional array search by conditions

I am developing a financial web application I am really stuck with this issue.
I need to search with conditions and return the array if the condition is true..
I know the multi-dimensional array search, but I did't get any idea for this. For example :
if array[0][7] > array[0][8] && array[0][12] < array[0][15]
Please suggest me the solution
Array (
[0] => Array (
[0] => 54452
[1] => 'KSB'
[2] => 'INE999A01015'
[3] => 'EQ'
[4] => 'Ksb Limited'
[5] => -0.70
[6] => -0.10
[7] => 662.90
[8] => 663.60
[9] => 669.35
[10] => 678.25
[11] => 651.55
[12] => 7874
[13] => 676.91
[14] => 690.93
[15] => 703.61
[16] => 664.23
[17] => 650.21
[18] => 637.53
[19] => 623.51
[20] => 2530
[21] => 32.13
[22] => 825.00
[23] => 539.00
[24] => 665.89
[25] => 631.37
[26] => 616.65
[27] => 610.14
[28] => 615.89
[29] => 656.25
[30] => 680.74
[31] => 60.71
[32] => 60.71
[33] => 13.62
[34] => 29.39
[35] => -38.97
[36] => 129.47
[37] => 2019-09-26
[38] => 27632
),
[1] => Array (
[0] => 53772
[1] => 'ASPINWALL'
[2] => 'INE991I01015'
[3] => 'EQ'
[4] => 'Aspinwall And Company Limited'
[5] => -10.50
[6] => -7.74
[7] => 127.45
[8] => 137.95
[9] => 135.60
[10] => 144.50
[11] => 112.60
[12] => 9583
[13] => 143.76
[14] => 160.08
[15] => 175.66
[16] => 128.18
[17] => 111.86
[18] => 96.28
[19] => 79.96
[20] => 5108
[21] => 53.30
[22] => 214.80
[23] => 112.60
[24] => 135.17
[25] => 137.13
[26] => 137.46
[27] => 136.86
[28] => 137.59
[29] => 143.00
[30] => 153.89
[31] => 39.41
[32] => 39.41
[33] => -0.90
[34] => 37.96
[35] => -58.05
[36] => -361.91
[37] => 2019-09-26
[38] => 26324
)
)
Thanks in advance
Give a try to following code and make necessary changes according to your need
function search_sg_dma($array, $pre_close, $dma_value, $close_price){
...
$result = array();
for( $i = 0; $i < count( $array ); $i++ ) {
if ( $array[$i][$pre_close] < $array[$i][$dma_value] && $array[$i][$close_price] > $array[$i][$dma_value] ) {
$result[] = array[$i];
}
}
return $result;
}

How to preg_split all character, but don't split <b> and <br>

There are tons of questions about [preg_split] here, but none relates to my problem. I'm using the following code to split strings to characters in PHP, like this:
$str = "My <b>table</b> in brown <br> Help";
$char = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($char);
Output is:
Array
(
[0] => M
[1] => y
[2] =>
[3] => <
[4] => b
[5] => >
[6] => t
[7] => a
[8] => b
[9] => l
[10] => e
[11] => <
[12] => /
[13] => b
[14] => >
[15] =>
[16] => i
[17] => n
[18] =>
[19] => b
[20] => r
[21] => o
[22] => w
[23] => n
[24] =>
[25] => <
[26] => b
[27] => r
[28] => >
[29] => ...
)
But I expect the following:
Array
(
[0] => M
[1] => y
[2] =>
[3] => <b>
[6] => t
[7] => a
[8] => b
[9] => l
[10] => e
[11] => </b>
[15] =>
[16] => i
[17] => n
[18] =>
[19] => b
[20] => r
[21] => o
[22] => w
[23] => n
[24] =>
[25] => <br>
[29] => ...
)
Characters such as: <b>,</b>,<br>,<i>,</i> etc. shouldn't split.
Thank you.
You can do this by splitting on either a single character, or a sequence of characters within < and >, using the PREG_SPLIT_DELIM_CAPTURE option to capture each value:
$str = "My <b>table</b> in brown <br> Help";
$char = preg_split('#(</?[a-z]+>|[a-z ])#ui', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($char);
Output:
Array (
[0] => M
[1] => y
[2] =>
[3] => <b>
[4] => t
[5] => a
[6] => b
[7] => l
[8] => e
[9] => </b>
[10] =>
[11] => i
[12] => n
[13] =>
[14] => b
[15] => r
[16] => o
[17] => w
[18] => n
[19] =>
[20] => <br>
[21] =>
[22] => H
[23] => e
[24] => l
[25] => p
)
Demo on 3v4l.org

PHP fgetcsv fails while explode works fine

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---------

I need to get all commented data from inside html page vai php [duplicate]

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
)

PHP replace only first occurrence in String for each Array value

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

Categories