Last active
March 19, 2018 14:40
-
-
Save davidchc/d85ca60da6d5b4f0fca664030c6ad251 to your computer and use it in GitHub Desktop.
Classe para recuperar todas as imagens de um string, e recuperar partes dessa imagem
This file contains 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 | |
class ImageDOM | |
{ | |
private $content; | |
private $images; | |
private $src; | |
private $alt; | |
private $style; | |
private $strip; | |
public function __construct($content) | |
{ | |
preg_match_all('/<img[^>]*>/i',$content, $matches); | |
$this->images = $matches[0]; | |
$this->content = $content; | |
$this->src = []; | |
$this->alt = []; | |
$this->style = []; | |
} | |
protected function attr($flag) | |
{ | |
return array_map(function($image) use($flag){ | |
preg_match('/'.$flag.'="([^"]*)"/mis',$image, $matches); | |
return $matches[1] ?? ''; | |
} , $this->images); | |
} | |
public function src() | |
{ | |
$this->src = $this->attr('src'); | |
return $this; | |
} | |
public function alt() | |
{ | |
$this->alt = $this->attr('alt'); | |
return $this; | |
} | |
public function style() | |
{ | |
$this->style = $this->attr('style'); | |
return $this; | |
} | |
public function strip() | |
{ | |
$this->strip = preg_replace('/<img[^>]*>/i', '', $this->content); | |
return $this; | |
} | |
public function getData() | |
{ | |
$data = []; | |
if($this->src) { | |
$data['src'] = $this->src; | |
} | |
if($this->style) { | |
$data['style'] = $this->style; | |
} | |
if($this->alt) { | |
$data['alt'] = $this->alt; | |
} | |
if($this->content) { | |
$data['content'] = $this->content; | |
} | |
if($this->strip) { | |
$data['strip'] = $this->strip; | |
} | |
return $data; | |
} | |
} |
This file contains 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 | |
require_once "imageDOM.php"; | |
$content = 'jbajdbfkjsd <img src="endereco/imagem1.jpg" /> | |
<img src="endereco/imagem2.jpg" /> | |
<img src="endereco/imagem3.jpg" /> | |
<img src="endereco/imagem4.jpg"/> | |
'; | |
$image = new ImageDOM($content); | |
$result = $image->src()->strip()->getData(); | |
print_r($result); |
This file contains 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 | |
require_once "imageDOM.php"; | |
$content = 'jbajdbfkjsd <img src="endereco/imagem1.jpg" /> | |
<img src="endereco/imagem2.jpg" /> | |
<img src="endereco/imagem3.jpg" /> | |
<img src="endereco/imagem4.jpg"/> | |
'; | |
$result = (new ImageDOM($content))->src()->strip()->getData(); | |
print_r($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment