Find and spilit numbers from a string PHP - php

I have textarea, string :
__A 59.202x5p.
__B 611.08 500p
__C 991,70p.66.113.552.77.88.10p 199x200p
__C2 33 44x100p 55 161x150p 25 33 85x60p 727 77 373 22x220p
__C3 44 16 59x10p 343 x15p 172 200p
i want output like this :
__A 59.20 02x5p.
__B 61 11.08 500p
__C 99 91,70p.66.11 13.55 52.77.88.10p 19 99x200p
__C2 33 22 44x100p 55 16 61 x150p 25 33 85x60p 72 27 77 37 73 22x220p
__C3 44 16 59x10p 34 43 x15p 17 72 200p
If number is hundreds and before "x ? p" or " ?p" ( ? is random number and cant spilit ), it will spilit and line will like this :
__A 59.202x5p. >>> __A 59.20 02x5p.
__B 611.08 500p >>> __B 61 11.08 500p
__C 991,70p.66.113.552.77.88.10p 199x200p >>> __C 99 91,70p.66.11 13.55 52.77.88.10p 19 99x200p
...
I use preg_match + preg_replace + substr but i cant locate where is hundreds number before "x ? p" or " ?p" ( ? is random number and cant spilit )...
And i dont understand how to spilit number like :
__A 59."202"x5p. ( 202 to 20 02 ) >>> __A 59.20 02x5p.
__B 611.08 500p ( 611 to 61 11 ) >>> __B 61 11.08 500p
My English language not good, hope who read my question can understand and help me solve it.
Thank very very much.

Check the following code..
<?php
echo "<u>CURRENT STRING</u><br/>";
echo $value ="__A 59.202x5p.
__B 611.08 500p
__C 991,70p.66.113.552.77.88.10p 199x200p
__C2 33 44x100p 55 161x150p 25 33 85x60p 727 77 373 22x220p
__C3 44 16 59x10p 343 x15p 172 200p";echo '<br>';
for($i=0;$i<=(strlen($value)-4); $i++ ) {
$myvar = $value[$i].$value[$i+1].$value[$i+2];
if (preg_match("/\d{3}/u", $myvar) > 0 && $myvar>100 && strpos($myvar.$value[$i+3], 'p') == 0)
$value = substr($value,0,$i+2).' '.$value[$i+1].substr($value,$i+2,strlen($value));
}
echo "<u>DESIRED STRING</u><br/>";
echo $value;
?>

Related

php for loop in for loop

I have a problem trying using for loop inside a loop i can't get the result i want
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".echo $sg;."</b>";
for($g = 1; $g <= 11; $g++){
echo "<p>".echo $g."</p>";
}
?>
I looking the result to be like
1
1 2 3 4 5 6 7 8 9 10 11
2
12 13 14 15 16 17 18 19 20
3
21 22 23 24 25 26 27 28 29 30
continuous ....
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<br /> <b>". $sg."</b> ";
for($g = 1+(10*($sg-1)); $g <= (10*($sg-1))+10; $g++){
echo "". $g." ";
}
}
?>
Output:
1 1 2 3 4 5 6 7 8 9 10
2 11 12 13 14 15 16 17 18 19 20
3 21 22 23 24 25 26 27 28 29 30
4 31 32 33 34 35 36 37 38 39 40
5 41 42 43 44 45 46 47 48 49 50
6 51 52 53 54 55 56 57 58 59 60
7 61 62 63 64 65 66 67 68 69 70
8 71 72 73 74 75 76 77 78 79 80
9 81 82 83 84 85 86 87 88 89 90
10 91 92 93 94 95 96 97 98 99 100
11 101 102 103 104 105 106 107 108 109 110
<?php
$inner_start = 1;
$inner_max = 11;
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".$sg."</b></br>";
echo "<p>";
for($g = $inner_start; $g <= $inner_max; $g++){
echo $g . ' ';
}
$inner_start = $inner_start + 10;
$inner_max += 10;
echo "</p></br>";
}
?>

Upside down PHP array values?

I have array echo this: (2 5 7 13 19 22 23 37 41 41 64 74 85 96 139);
But I need this echo: (139 96 85 74 64 41 41 37 23 22 19 13 7 5 2);
I can't find, how to upside down echo value?
$num= array(7,13,85,64,2,41,22,96,139,37,41,19,74,23,5);
$max= max($num);
$a= count($num);
sort($num);
for ($x=0; $x < $a; $x++) {
echo $num[$x]. " ";
}
output: 2 5 7 13 19 22 23 37 41 41 64 74 85 96 139
Replace sort($num); with rsort($num);.
sort() sorts from low to high.
rsort() sorts from high to low.
use array rsort()
$output=rsort($num);
If array is already sorted, then use array_reverse. It is more performant than sorting array in descending order.
$reversedOrderArray = array_reverse($yourArray);

Is there a way to remove two numbers not affecting others?

The code is giving the following output and each time, it will give a different output.
Is there any way to remove to 2 numbers for example 15 and 60, so that other numbers are not effected they remain the same?
I have tried but could not get the desired result.
Any help or any idea how to do this?
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Out put
52 67 5 44 76 49 1 27 28
73 33 19 66 4 14 63 45 62
26 75 50 80 70 38 12 54 78
9 69 36 32 2 7 56 11 51
40 20 22 15 60 65 31 41 77
57 29 34 79 68 23 18 71 39
42 10 72 17 81 30 35 48 47
37 59 53 6 3 55 13 46 58
61 25 24 64 74 43 21 8 16
This should work for you:
Just place the code before your shuffle() call. Basically first we create an array of patterns, by looping through all numbers, which we want to extract from the list, with array_map().
The pattern basically has two anchors to match the numbers fully in your array and not only partially (E.g. 2 will not match 24, but it will match 2).
With the created pattern array we just use preg_replace() to replace the numbers in your array with an empty string.
$numbers = [15, 60];
$pattern = array_map(function($v){
return "/^$v$/";
}, $numbers);
$arr = preg_replace($pattern, "", $arr);

How to show these numbers in tabular format?

This code is giving the following output, I want to show these numbers in table is there any way to do this? and why it is not showing the numbers in proper format like why 53 is not exactly below 40 and others also not showing in proper order?
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("&nbsp&nbsp&nbsp", $line);
}
echo implode("<br>", $lines);
?>
Output
73 40 79 1 43 7 76 44 18
6 53 45 55 71 20 80 66 74
69 51 52 65 22 63 59 50 54
29 33 23 49 77 24 61 60 58
8 81 30 15 26 32 16 47 31
17 39 4 35 27 11 5 25 68
2 34 72 42 75 46 48 3 38
14 28 37 62 10 78 12 56 13
41 21 19 36 9 64 67 57 70
If you don't want to use tables or CSS. You can use a <pre> tag then use some tabs and newlines
<pre>
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("\t", $line);
}
echo implode("\n", $lines);
?>
Fiddle
Note: I didn't really care about your logic for creating those lines while answering this question since its only about formatting. You can trim down some code too.
Output
22 16 66 79 8 41 47 2 80
29 38 76 18 40 46 73 34 45
31 3 62 68 14 33 20 72 67
78 44 42 30 51 77 36 25 48
64 70 21 15 19 9 56 50 65
37 27 4 1 35 74 75 52 32
81 23 10 28 26 59 7 54 11
6 63 5 39 53 12 24 60 49
71 55 17 13 61 69 43 57 58
Now with slightly better code
<pre>
<?php
$numbers=range(1,81);
shuffle($numbers);
$c=0;
foreach($numbers as $n)
{
if($c%9==0)echo "\n";
echo $n."\t";
$c++;
}
?>
modify your code like this:
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo '<table>';
foreach ($lines as $key => $line) {
echo '<tr><td align="right">';
echo $lines[$key] = implode('</td><td align="right">', $line);
echo '</td></tr>';
}
echo '</table>';
?>
Output
57 41 48 17 73 76 7 78 12
69 61 39 80 24 58 45 11 70
47 65 33 21 38 4 19 13 46
59 52 63 14 25 3 30 28 77
50 40 68 6 2 29 20 66 26
72 74 34 75 15 36 71 10 60
55 53 1 16 23 42 51 35 62
44 32 43 64 18 8 54 49 5
81 27 31 67 37 22 79 56 9
try this
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Your Out put

Was ending character reached or not

In summary I am using stream_get_line to read a line of a file, replace a string and then write the line to another file.
I am using stream_get_line and supplying the "ending" parameter to instruct the function to read lines, or if there is no new line then read 130 bytes.
What I would like to know is how can I know if the 3rd parameter (PHP_EOL) was found, as I need to write exactly the same line (except for my string replacement) to the new file.
For reference...
string stream_get_line ( resource $handle , int $length [, string $ending ] )
It's mainly needed for the last line, sometimes it will contain a newline character and sometimes it doesn't.
My initial idea is to seek to the last line of the file and search the line for a new line character to see if I need to attach a newline to my edited line or not.
You could try using fgets if the stream is in ASCII mode (which only matters on Windows). That function will include the newline if it is found:
$line = fgets(STDIN, 131);
Otherwise, you could use ftell to see how many bytes were read and thus determine whether there was a line ending. For example, if foo.php contains
<?php
while (!feof(STDIN)) {
$pos = ftell(STDIN);
$line = stream_get_line(STDIN, 74, "\n");
$ended = (bool)(ftell(STDIN) - strlen($line) - $pos);
echo ($ended ? "YES " : "NO ") . $line . "\n";
}
executing echo -ne {1..100} '\n2nd to last line\nlast line' | php foo.php will give this output:
NO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
NO 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5
NO 3 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
YES 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
YES 2nd to last line
NO last line

Categories