| 1 |
<?php |
| 2 |
namespace Averta\WordPress\Utility; |
| 3 |
|
| 4 |
|
| 5 |
class Extract |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Extract all images from content |
| 9 |
* |
| 10 |
* @param string $text The text to extract images from. |
| 11 |
* |
| 12 |
* @return bool|array List of images in array |
| 13 |
*/ |
| 14 |
public static function imagesFromText( $text ) |
| 15 |
{ |
| 16 |
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $text, $matches ); |
| 17 |
return isset( $matches ) && count( $matches[0] ) ? $matches : false; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get first image src from content |
| 22 |
* |
| 23 |
* @param string $text The content to extract image from. |
| 24 |
* |
| 25 |
* @return string First image URL on success and empty string if nothing found |
| 26 |
*/ |
| 27 |
public static function firstImageSrcFromText( $text ) |
| 28 |
{ |
| 29 |
$images = self::imagesFromText( $text ); |
| 30 |
|
| 31 |
return ( $images && count( $images[1]) ) ? $images[1][0] : ''; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get first image tag from string |
| 36 |
* |
| 37 |
* @param string $text The text to extract image from. |
| 38 |
* |
| 39 |
* @return string First image tag on success and empty string if nothing found |
| 40 |
*/ |
| 41 |
public static function firstImageFromText( $text ) |
| 42 |
{ |
| 43 |
$images = self::imagesFromText( $text ); |
| 44 |
|
| 45 |
return ( $images && count( $images[0]) ) ? $images[0][0] : ''; |
| 46 |
} |
| 47 |
} |
| 48 |
|