Skip to content

Instantly share code, notes, and snippets.

@jgmuchiri
Created February 2, 2019 05:57
Show Gist options
  • Save jgmuchiri/cad5739ea1e073662b898e1cb22db420 to your computer and use it in GitHub Desktop.
Save jgmuchiri/cad5739ea1e073662b898e1cb22db420 to your computer and use it in GitHub Desktop.
Extracts attribute values inside an element from html content
<?php
/**
* You can modify preg_match_all to match your needs
*
* Extracts attribute values inside an element from html content
* You post html content and inside the content, you have shortcodes with attributes like [pageBreak id="home" title="Home page"]
* You want to extract id and title attribute values so you can save them in db as a matching pair.
*/
$content = 'hello there [pageBreak ="home" title="Home page"] new page [/pageBreak] another page';
extract($content);
function extract($content){
preg_match_all("/\[pageBreak id=\"(.*?)\" title=\"(.*?)\"\]/", $content,$navs);
$n = array();
foreach( $navs[0] as $nav)
{
preg_match_all('/(id|title)=("[^"]*")/i',$nav, $n[$nav]);
}
foreach($n as $i){
$nav=$i[2][0];
$title=$i[2][1];
print($nav.'='.$title);
echo '</br>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment