![]()
Snippets of A |
|
|
What are snippets? |
|
strip_tags does) but of returning the offset positions where the text wrapped within tags is located.
strip_tags) retaining the manipulations.
function stripTagsOffsets($input=''){ $opentags=substr_count($input, '<'); $closedtags=substr_count($input, '>'); if(!$opentags || !$closedtags){return array(array(strlen($input), 0));}; $array=array(); $afterLastClosedTagPosition=-1; $beforeNextOpenTagPosition=strpos($input, '<'); for($i=0; $i<$opentags; $i++){ $array[]=array($beforeNextOpenTagPosition, ++$afterLastClosedTagPosition); $beforeNextOpenTagPosition=strpos($input, '<', ++$beforeNextOpenTagPosition); $afterLastClosedTagPosition=strpos($input, '>', ++$afterLastClosedTagPosition); } array_push($array, array( strlen($input), ++$afterLastClosedTagPosition )); return $array;/*keep this comment to reuse freely http://www.fullposter.com/?1*/}Remove colors
<);
>)
function usageExample($input=''){/*INCLUDE FUNCTION: stripTagsOffsets. This is a MERE example function, assuming the passed argument is in the shape returned by stripTagsOffsets*/ $outcome=stripTagsOffsets($input); print_r($outcome); print '<hr>'; $L=sizeof($outcome); $output=array(); $n=0; for(; $n<$L; $n++){ $output[]='<xmp>'. /*Offsets to use to grab tag BEFORE:*/substr($input, $outcome[$n-1][0], $outcome[$n][1]-$outcome[$n-1][0]). "</xmp>\n ". /*Offsets to use to grab text, plus: ACTION instance: replacing 'e' in TEXT only NOT in tags:*/str_replace('e', 'EEE', substr($input, $outcome[$n][1], $outcome[$n][0]-$outcome[$n][1])). "\n<xmp>". /*Offsets to use to grab tag AFTER:*/substr($input, $outcome[$n][0], $outcome[$n+1][1]-$outcome[$n][0]) ."</xmp><hr />"; }print implode('', $output);} $meaninglessText='This string begins with an <em>em tagged text</em> and <span><span></span></span>foo foo <strong>blod with <em>nested em</em></strong> and anchor to say: go <a href="http://www.dunno.com">click here to visit dunno.com</a> etc etc an image here <div style="text-align:center"><img src="fooem.gif" alt="" /> a photo caption</div> plus a break line <br /> and more text with this <strong><em>bold and em once</em></strong> and empty tag <strong></strong> and empty nested tags <strong><em></em></strong> true end here.'; usageExample($meaninglessText);Remove colors