How to combine only the first number in a string, and not the full one?

I am currently having a problem with my code:

           setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
           $date = new DateTime(helper('com://site/ohanah.date.format', array(
               'date' => $event->start,
               'format' => 'Y-m-d H:i',
               'timezone' => 'UTC'
           )));

           $date_scn = new DateTime(helper('com://site/ohanah.date.format', array(
               'date' => $event->end,
               'format' => 'H:i',
               'timezone' => 'UTC'
           )));

           echo '<div id="my-id" class="uk-modal"><div class="uk-modal-dialog"><a class="uk-modal-close uk-close"></a><div style="font-weight: 400;font-size:18px;padding-bottom: 10px;">text 1</div><hr><br/>';

           echo '<span style="font-weight: 400;">1. text: </span>'.helper('com://site/ohanah.date.format', array('date' => $event->start, 'format' => 'l, j. F Y, H:i','timezone'=>'UTC')), ' - ', helper('com://site/ohanah.date.format', array('date' => $event->end, 'format' => 'H:i', 'timezone'=>'UTC')).'</span><br>';


             $cnt = 2; // start the text to count at two as the first one is already defined above

             $raw_amount = object('com://site/ohanah.controller.ticket_type')->ohanah_event_id($event->id)->render(array('event' => $event));

             $filtered_output = strip_tags($raw_amount);

             $filtered_numbers = array_filter(preg_split("/\D/", $filtered_output)); // this line causes the problem
             $first_occurence  = reset($filtered_numbers);

             $filtered_amount = $first_occurence - 1; // subtract 1 from $filtered_output as it is always one more

             for($i=0;$i<$filtered_amount;$i++) { // loop
                 $date->add(new DateInterval('P1W')); //add one week

                 $formatted_time = strftime("%A, %d. %B %Y, %H:%M", $date->getTimestamp());
                 $formatted_time_scnpart = strftime("%H:%M", $date_scn->getTimestamp());
                 // This is the modal
                echo '<span style="font-weight:400;">'.$cnt++.'. '.'Termin: '.'</span>';
                echo '<span>'.$formatted_time.' - '.$formatted_time_scnpart.'</span>';
                echo '<br/>';
              }
              echo '</div></div>';

I already commented on the line with the problem ( // this line causes the problem)

How does this code:
$raw_amount displays the html with some comments, I filter these unwanted tags using: $filtered_output = strip_tags($raw_amount);.

This refers to my line that causes problems:

 $filtered_numbers = array_filter(preg_split("/\D/", $filtered_output));

and then delete 1from the output. I do this because the output is $first_occurencetoo large 1(due to the way I import the data into it.)

, ?
$filtered_numbers 987654321, , 9. , , , $raw_output, <strong>, , . ?

$filtered_output, .:

            if (typeof Koowa === 'object' && Koowa !== null) {
                if (typeof Koowa.translator === 'object' && Koowa.translator !== null) {
                    Koowa.translator.loadTranslations({"KLS_OHANAH_TICKET_GET":"Tickets beziehen","KLS_OHANAH_PAY_OFFLINE":"Buchung absenden","KLS_OHANAH_PAY_WITH_PAYPAL":"Mit PayPal bezahlen","KLS_OHANAH_TICKET_PAY":"Kurs bezahlen"});
                }
            }

            example1
                        example2
            Persons

                event - 5 x 15 min

                1241,2423

0
1
2

                event - 4 x 78 min (example)

                1241241,2423

0
1
2
+4
1

preg_split , '13 lskjha12 jsdh 3445' :

Array
(
    [0] => 13
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 12
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [12] => 
    [13] => 3445
)

array_filter :

Array
(
    [0] => 13
    [7] => 12
    [13] => 3445
)

( ) , - array_map:

$filtered_numbers = array_map(function ($s) { return substr($s, 0, 1); }, array_filter(preg_split('/\D/', $filtered_output)));

:

Array
(
    [0] => 1
    [7] => 1
    [13] => 3
)

,

$filtered_numbers = array_filter(preg_split('/\D/', $filtered_output));
$filtered_numbers = substr($filtered_output[0], 0, 1);
0

Source: https://habr.com/ru/post/1695902/


All Articles