php - preg match and stripos -
i have csv import function. there in csv column ''1. informatik, bachelor, 2015, 1. fachsemester'' or '' 1. mathematik, la bachelor gymnasien, 2015,''
if in line exists 'la' , $fachrichtung='lehramt'
, if there no la, $fachrichtung
first word after number. here: 1. informatik, bachelor, 2015, 1. fachsemester $fachrichtung= 'informatik'
. if first word ist not informatik or physik, $fachrichtung= 'sonstige'
.
preg_match( '/[\d]+\.(?p<fach>[\w\s]+)/ius', $tmp[6], $amatches ); $fachrichtung = ( false !== stripos($tmp[6], ', la ') ) ? "lehramt" : trim( $amatches['fach'] );
how can include last condition ('sonstige') in code above? tried if , else doesn't function. thanks
you need check value of fach
group later , assign value $fachrichtung
accordingly:
// $tmp = '1. mathematik, la bachelor gymnasien, 2015,'; // => sonstige $tmp = '1. informatik, bachelor, 2015, 1. fachsemester'; // => informatik $fachrichtung = ''; if (preg_match( '/\d+\.(?p<fach>[\w\s]+)/ius', $tmp, $amatches )) { if (true === stripos($tmp, ', la ') ) { $fachrichtung = "lehramt"; } else if ("informatik" !== trim( $amatches['fach'] ) && "physik" !== trim( $amatches['fach'] )) { $fachrichtung = "sonstige"; } else { $fachrichtung = trim( $amatches['fach'] ); } echo $fachrichtung; }
see php demo.
the regex ok, added couple of if ... else
.
the else if ("informatik" !== trim( $amatches['fach'] ) && "physik" !== trim( $amatches['fach'] ))
checks if fach
group value not equal trimmed informatik
or physik
. if not equal, set sonstige
value. else, set fach
group value.
Comments
Post a Comment