That function extract paragraph based on specific keywords
Last active
May 14, 2016 19:37
-
-
Save rhcarlosweb/4ea46af956a2b30c3d24 to your computer and use it in GitHub Desktop.
Extract paragraph based on keywords
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 | |
// conteudo artigo | |
$content = get_the_content(); | |
// pega paragrafos do conteudo no artigo | |
$start = strpos($content, '<p>'); | |
$end = strpos($content, '</p>', $start+3); | |
$paragraph = substr($content, $start, $end-$start-3); | |
$paragraph = html_entity_decode($paragraph); | |
// palavras chaves | |
$palavra_chave = array('Sinopse e detalhes', 'Descrição', 'Descrição e detalhes'); | |
$pos = 0; | |
while ($pos < strlen($paragraph)) { | |
$start = strposa($paragraph, $palavra_chave, $pos); | |
$end = strposa($paragraph, $palavra_chave, $pos+18); | |
if($start === false || $end === false) { | |
break; | |
} | |
// paragrafo completo com palavras chaves | |
$contents = substr($paragraph, $start); | |
echo strip_tags($contents); | |
break; | |
} |
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 | |
/* ========================================================================== | |
Função para array de strpos | |
========================================================================== */ | |
function strposa($haystack, $needles=array(), $offset=0) { | |
$chr = array(); | |
foreach($needles as $needle) { | |
$res = strpos($haystack, $needle, $offset); | |
if ($res !== false) $chr[$needle] = $res; | |
} | |
if(empty($chr)) return false; | |
return min($chr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment