`
* @return string Cleaned String, e.g. `foobar`
*
- * @todo This function needs more documentation to make it maintainable.
+ * This function works by locating the desired tag start, then finding the appropriate
+ * end by counting opening and ending tags until the amount of open tags reaches zero:
+ *
+ * ```
+ * Amount of open tags:
+ * 1 2 1 0
+ * |---------------||---| |----| |----|
+ *
bar
+ * | <-------- Section to remove -------> |
+ * ```
*/
function stripRecursiveHTMLSection($string, $tag_name, $tag_start)
{
$open_tag = '<' . $tag_name;
$close_tag = '' . $tag_name . '>';
$close_tag_length = strlen($close_tag);
+
+ // Make sure the provided $tag_start argument matches the provided $tag_name argument
if (strpos($tag_start, $open_tag) === 0) {
+ // While tag_start is present, there is at least one remaining section to remove
while (strpos($string, $tag_start) !== false) {
+ // In order to locate the end of the section, we attempt each closing tag until we find the right one
+ // We know we found the right one when the amount of "
$close_tag_count && $max_recursion > 0);
+ // We exited the loop, let's remove the section
$string = str_replace($section_to_remove, '', $string);
}
}