Regex

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text.

Examples

Wildcard search in array with regex in PHP

$array = array('Michael','Marian','Martina');
$matching_letters = 'ri'; 
$array = preg_grep("/{$matching_letters}/i", $array);

Websites for regex pattern matching

Regex101.com 
PHPLiveRegex.com 

Sort Multidimensional Array by Value

$list = Array
(
    [0] => Array
        (
            [lat] => '43.65529'
            [lng] => '-79.38397'
            [name] => 'Name'
        )
    [1] => Array
        (
            [lat] => '43.65328'
            [lng] => '-79.37947'
            [name] => 'Name'
        )
    [2] => Array
        (
            [lat] => '43.65516'
            [lng] => '-79.38892'
            [name] => 'Name'
        )
)

Sort with latitude going smallest to largest with longitude also going smallest to largest after

array_multisort(
	array_column($list, 'lat'), SORT_ASC, 
	array_column($list, 'lng'), SORT_ASC,
	$row
);

Array now looks like this

Array
(
    [0] => Array
        (
            [lat] => '43.65328'
            [lng] => '-79.37947'
            [name] => 'Name'
        )
    [1] => Array
        (
            [lat] => '43.65516'
            [lng] => '-79.38892'
            [name] => 'Name'
        )
    [2] => Array
        (
            [lat] => '43.65529'
            [lng] => '-79.38397'
            [name] => 'Name'
        )
)

Embed Video Player

Video Class code attached. You can put this file in any directory. Previously it was put in a folder called classes.

Calling video class

$link = "https://www.youtube.com/watch?v=NpEaa2P7qZI";
$embed = Video::get_embed_URL($link)."?&autoplay=1&loop=1";

Video Class

class Video {
const _youtube_regex = '/(\/\/.*?youtube\.[a-z]+)\/watch\?v=([^&]+)&?(.*)/';
const _youtubeshort_regex = '/(\/\/.*?youtu\.be)\/([^\?]+)(.*)/i';
const _vimeo_regex = '/(\/\/.*?)vimeo\.[a-z]+\/([0-9]+).*?/';

/**
 * Given filetype column from DB's article_medias table will return true if the media is video
 * @param string $source source from article_medias.filetype column
 * @return bool True if video
 */
public static function is_video( $source ) {
	if ($source == 'youtube' || $source == 'youtu.be' || $source == 'vimeo') {
		return true;
	} else {
		return false;
	}
}


/**
 * Given a url param to a youtube or vimeo video, will return the related format
 * @param string $url Url to the video
 * @return bool|string
 */
public static function get_source( $url ) {
	if (preg_match(self::_youtube_regex, $url)) {
		$format = 'youtube';
	} else if (preg_match(self::_youtubeshort_regex, $url)) {
		$format = 'youtu.be';
	} else if (preg_match(self::_vimeo_regex, $url)) {
		$format = 'vimeo';
	} else {
		return false;
	}

	return $format;
}



/**
 * Given a video url to either a youtube or vimeo video, will return the specific video id
 * @param string $url Video url
 * @return bool|string
 */
public static function get_source_ID( $url ) {
	$format = self::get_source($url);

	switch ($format) {
		case 'vimeo':
			$vid_id = preg_replace(self::_vimeo_regex, '$2', $url);
			if (substr($vid_id, 0, 5) == "http:") { $vid_id = substr($vid_id, 5); }
			if (substr($vid_id, 0, 6) == "https:") { $vid_id = substr($vid_id, 6); }
			break;

		case 'youtube':
			$vid_id = rtrim(preg_replace(self::_youtube_regex, '$2', $url), '?');
			if (substr($vid_id, 0, 5) == "http:") { $vid_id = substr($vid_id, 5); }
			if (substr($vid_id, 0, 6) == "https:") { $vid_id = substr($vid_id, 6); }
			break;

		case 'youtu.be':
			$vid_id = rtrim(preg_replace(self::_youtubeshort_regex, '$2', $url), '?');
			if (substr($vid_id, 0, 5) == "http:") { $vid_id = substr($vid_id, 5); }
			if (substr($vid_id, 0, 6) == "https:") { $vid_id = substr($vid_id, 6); }
			break;

		default:
			return false;
	}
	return $vid_id;
}


 /**
	* Gets the html url for the video
	* @param string $id Video id
	* @param string $format Video format/source
	* @return bool|string returns url for embedding the video
	*/
public static function get_embed_URL($url) {
	$format = self::get_source($url);
	$id = self::get_source_ID($url);

 switch ($format) {
	 case 'vimeo':
		 $link = 'https://player.vimeo.com/video/'.$id;
		 break;

	 case 'youtube':
		 $link = 'https://www.youtube.com/embed/'.$id;
		 break;

	 case 'youtu.be':
		 $link = 'https://www.youtube.com/embed/'.$id;
		 break;

	 default:
		 return false;
 }

 return $link;
}

}