Skip to content

Instantly share code, notes, and snippets.

@JoshuaEstes
Created April 22, 2012 23:18
Show Gist options
  • Select an option

  • Save JoshuaEstes/2467460 to your computer and use it in GitHub Desktop.

Select an option

Save JoshuaEstes/2467460 to your computer and use it in GitHub Desktop.
Break apart an html string, keep the tags and positions
<?php
$text = <<<EOF
<p>there is more to it <i>than</i> this.</p>
<div class="test">more test stuff.</div>
<p>Last bit of text in a paragraph.</p>
<p>Continuing to test this.</p>
EOF;
$plainText = strip_tags($text);
// Get all of the html tags
preg_match_all('/<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\\\"|.*?|[^">\s]+))?)+\s*|\s*)\/?>/s', $text, $matches, PREG_PATTERN_ORDER);
//var_dump($matches[0]);die();
// Create an array that contains the tag and the position
// where it needs to be entered at
$intOffset = 0;
$intIndex = 0;
$intTagPositions = array();
foreach ($matches[0] as $tag){
$intTagPositions[$intIndex] = array(
'tag' => $tag,
'start' => (int) strpos($text, $tag, $intOffset),
);
$intOffset = (int) strpos($text, $tag, $intOffset) + strlen($tag);
$intIndex++;
}
// Dump the array
//var_dump($intTagPositions);die();
// The the array of where each tag needs to be placed and
// put it into the plain text
$newString = $plainText;
foreach ($intTagPositions as $tag){
$part1 = substr($newString, 0, $tag['start']);
$part2 = substr($newString, $tag['start']);
$part1 = $part1 . $tag['tag'];
$newString = $part1 . $part2;
}
var_dump($plainText,$newString);
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment