2016-08-11 4 views
0

Kann ich eine dieser Zeichenfolgen in Array mit explode() analysieren?Parse alle Tage auf Zeichenfolge in PHP

Das Array aussehen wie dieses

$array = [ 
    'North AmericaWed 20:00', 
    'North AmericaWed 20:00', 
    'New ZealandTue 14:00', 
    'IndonesiaThu 08:00' 
]; 

ich ohne daran befestigten Tage Ländernamen erhalten möchten.

Dies ist der php Code, den ich gedacht:

foreach($array as $string){ 
    $parse = explode('any days' , $string); 
    $result = $parse[0]; 
} 

and the result is : 

North America 
New Zealand 
Indonesia 

Danke für die Hilfe jeder!

+1

versuchen, die Zeichenfolge mit Regex zu analysieren – Ghost

Antwort

2
$array = [ 
    'North AmericaWed 20:00', 
    'North AmericaWed 20:00', 
    'New ZealandTue 14:00', 
    'IndonesiaThu 08:00' 
]; 

foreach($array as $string){ 
    $country = preg_replace('/[0-9]*:[0-9]*+/', '', $string); 
    echo substr($country,0,-4).'<br/>'; 
} 
+0

ahh, vielen Dank, ich glaube nicht, Regex kann es in früh gelöst, noch einmal vielen Dank Mann –

1

Es ist sehr seltsam Code aber, wenn Sie das wirklich brauchen, hier ist die Art und Weise

<?php 
$array = [ 
    'North AmericaWed 20:00', 
    'North AmericaWed 20:00', 
    'New ZealandTue 14:00', 
    'IndonesiaThu 08:00' 
]; 
$countries = array(); 
foreach($array as $string){ 
    //$parse = explode('Wed ' , $string); 
    $parse = preg_split('/Mon |Tue |Wed |Thu |Fri |Sat |Sun /',$string); 
    $countries[] = $parse[0]."\n"; 
} 
$countries = array_unique($countries); 
print_r($countries); 
?> 

prüfen Demo: https://eval.in/620874

Ausgang ist:

Array 
(
    [0] => North America 

    [2] => New Zealand 

    [3] => Indonesia 

) 

schlage ich Ihre ändern Code von wo Sie über Zeichenfolge erstellen (country+day and time)