Skip to content

Instantly share code, notes, and snippets.

@aldolat
Last active January 8, 2023 11:02
Show Gist options
  • Save aldolat/7fbc4c0c626c9ea8cff5db91f026cd8f to your computer and use it in GitHub Desktop.
Save aldolat/7fbc4c0c626c9ea8cff5db91f026cd8f to your computer and use it in GitHub Desktop.
Compare a string and an array and return the common elements as a string.
<?php
/**
* Compare a string and an array and return the common elements as a string.
*
* @param string $string The string to be compared.
* @param array $array The array to be compared.
*
* @return string $output The string containing the common values.
*
* @since 3.8.8
*/
function pis_compare_string_to_array($string = '', $array = array())
{
// Convert the string to lowercase
$string = strtolower($string);
// Remove any space from the string
$string = str_replace(' ', '', $string);
// Remove any comma at the beginning and at the end of the string
$string = trim($string, ', ');
// Convert the string into an array
$string = explode(',', $string);
// Compare the two arrays and return the intersection (the common values)
$output = array_intersect($string, $array);
// Convert the returned array into a string
$output = implode(', ', $output);
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment