Last active
June 11, 2020 23:15
-
-
Save maheshwaghmare/2659480d590865542a9ebf82c6cfd61b to your computer and use it in GitHub Desktop.
Get image & other links from the given content.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get image & other links from the given content. | |
* | |
* @todo Change the `prefix_` and with your own unique prefix. | |
* | |
* @since 1.0.0 | |
* @return array | |
*/ | |
if( ! function_exists( 'prefix_get_links_from_content' ) ) : | |
function prefix_get_links_from_content( $content = '', $group = false ) { | |
$links = array(); | |
$content = stripslashes( $content ); | |
// Extract all links. | |
preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $content, $match ); | |
$all_links = array_unique( $match[0] ); | |
// Not have any link. | |
if ( empty( $all_links ) ) { | |
return array(); | |
} | |
// Get all links in group? | |
if( false === $group ) { | |
return $all_links; | |
} | |
$links = array( | |
'images' => array(), | |
'other' => array(), | |
); | |
// Extract normal and image links. | |
foreach ( $all_links as $key => $link ) { | |
if ( preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-]+\.(jpg|png|gif|jpeg)\/?$/i', $link ) ) { | |
$links['images'][] = $link; | |
} else { | |
// Collect other links. | |
$links['other'][] = $link; | |
} | |
} | |
return $links; | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment