I am wondering if you have a string, how would you define a separator?
We know that php can break a string using explode (), which requires a separator parameter.
But what about the delimiter detection method before sending it to the break function?
Now I just output the line to the user and enter the delimiter. This is great - but I'm looking for a template application that recognizes for me.
Should I look for regular expressions for this type of pattern recognition in a string?
EDIT: I was unable to initially indicate that there is a likely expected set of delimiters. Any delimiter that is probably used in CSV. Therefore, technically, anyone can use any character to delimit the CSV file, but it is more likely to use one of the following characters: comma, semicolon, vertical bar and space.
EDIT 2: Here is a feasible solution that I came up with for the “specific delimiter”.
$get_images = "86236058.jpg 86236134.jpg 86236134.jpg";
$probable_delimiters = array(",", " ", "|", ";");
$delimiter_count_array = array();
foreach ($probable_delimiters as $probable_delimiter) {
$probable_delimiter_count = substr_count($get_images, $probable_delimiter);
$delimiter_count_array[$probable_delimiter] = $probable_delimiter_count;
}
$max_value = max($delimiter_count_array);
$determined_delimiter_array = array_keys($delimiter_count_array, max($delimiter_count_array));
while( $element = each( $determined_delimiter_array ) ){
$determined_delimiter_count = $element['key'];
$determined_delimiter = $element['value'];
}
$images = explode("{$determined_delimiter}", $get_images);
source
share