<?php function extract_unit($string, $start, $end) { $pos = stripos($string, $start); $str = substr($string, $pos); $str_two = substr($str, strlen($start)); $second_pos = stripos($str_two, $end); $str_three = substr($str_two, 0, $second_pos); $unit = trim($str_three); // remove whitespaces return $unit; } $str = "X_HD01_1"; $str = "X_HD01_1, X_HD0003_2, X_HD12/12/2016/1_01"; $str_array =explode(',',$str); foreach($str_array as $val) { $unit = extract_unit($val, '_', '_'); // Outputs: acronym echo $unit."\n"; }
You have javascript disabled. You will not be able to edit any code.