php - Using preg_match or regx to replace a lot of words inside a html -
is there solution me convert html of following format
<span xmlns:v="http://rdf.data-vocabulary.org/#"> <span typeof="v:breadcrumb"> <a href="http://link1.com/" rel="v:url" property="v:title">home</a> </span> / <span typeof="v:breadcrumb"> <a href="http://link2.com/" rel="v:url" property="v:title">child 2</a> </span> / <span typeof="v:breadcrumb"> <a href="http://link3.com/" rel="v:url" property="v:title">child 3</a> </span> / <span typeof="v:breadcrumb"> <span class="breadcrumb_last" property="v:title">child 4</span> </span> </span>
into
<span itemscope="" itemtype="http://data-vocabulary.org/breadcrumb"> <span typeof="v:breadcrumb"> <a href="http://link1.com/" itemprop="url"> <span itemprop="title">home</span> </a> </span> / <span typeof="v:breadcrumb"> <a href="http://link2.com/" itemprop="url"> <span itemprop="title">child 2</span> </a> </span> / <span typeof="v:breadcrumb"> <a href="http://link3.com/" itemprop="url"> <span itemprop="title">child 3</span> </a> </span> / <span> <span class="breadcrumb_last"> <span itemprop="title">child 4</span> </span> </span> </span>
with php? want convert bread crump structure in rdfa microdata. thank help
the solution regexp, works example code, when attribute order changes fails:
$pattern = '#(?:rel\=\"v\:url\"\)? property\=\"v\:title\"\>([^\<]*)\<#ui'; $replacement = ' itemprop="url"><span itemprop="title">$1</span><'; $output = preg_replace($pattern,$replacement,$original);
if possible, think of html/xml parsing when want manipulate html/xml source, here powerful tool: https://code.google.com/p/phpquery/. if use jquery js framework, tool easy ;) see:
require_once 'phpquery/phpquery.php'; $dom = phpquery::newdocument($original); foreach($dom->find('a[rel="v:url"]') &$item){ $txt = $this->text(); $item-> removeattr('rel')-> removeattr('property')-> attr('itemprop','url')-> html("<span itemprop=\"title\">$txt</span>"); } $output = "$original";
Comments
Post a Comment