Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
Jap, ist dochn schönes Teil geworden.Passt ja ungefähr, und das hier scheint ja erledigt zu sein (dem Snippets Thread nach):
Ihh wasn das?Wie wäre es noch mit einer Funktion, um nicht geschlossene Tags auch zu entfernen?
$str="<p><b>Sofort gelöscht<br>";
$tags = Array('b','p');
echo remove_empty_tags ($str,$tags);
// ausgabe: <p><b>Sofort gelöscht
<p><b>Sofort gelöscht<br >
</b></p>
<p><b>Sofort gelöscht</p>
Irgendwie habe ich bloss verbummelt, warum ich da ein Blank haben wollte, da hätte ich besser argumentieren müssen. Fehlt einfach nur das \s von meiner früheren Version. Habe ich gleich überall hineineditiert. <-- Erledigt. (Auch im Snippets-Thread )Der entfernt <br /> weil er es als <b /> ekennt
Jap vielen Dank. Bist ja fast schneller als ich mit fixen.
Oder auch automatisch zu schließen, was ja auch nicht schlecht wäre. Das dürfte eventuell eine Lösung dazu sein, wobei per Parameter gesteuert wird, ob entfernt oder geschlossen wird.Wie wäre es noch mit einer Funktion, um nicht geschlossene Tags auch zu entfernen?
// Nicht geschlossene Tags entfernen oder schließen
function handle_unclosed_tags ($string, $close=false) {
$p_tag = '<[^>]*[^\/]>';
$split =
preg_split('/('.$p_tag.')/', $string, -1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$p_id = '[A-Za-z_\-][A-Za-z0-9_\-]*';
$p_o_long_tag = '<('.$p_id.')(?:\s[^>]*[^\/])?>';
$p_cl_long_tag = '<\/'.$p_id.'>';
$openTags = array();
$stringArray = array();
foreach ($split as $i => $elem) {
$stringArray[$i] = array($elem, true);
$match = array();
if (preg_match('/'.$p_o_long_tag.'/AS', $elem, $match)) {
$stringArray[$i] = array($elem, false);
array_unshift($openTags, array('</'.$match[1].'>', $i));
} elseif (preg_match('/'.$p_cl_long_tag.'/AS', $elem)) {
$stringArray[$i] = array($elem, false);
$closeTags = "";
for (
$openTag = array_shift($openTags);
isset($openTag[0]) && $elem != $openTag[0];
$openTag = array_shift($openTags)
) {
if ($close) {
$closeTags .= $openTag[0];
$stringArray[$openTag[1]][1] = true;
}
}
if (isset($openTag[0]) && $elem == $openTag[0]) {
$stringArray[$openTag[1]][1] = true;
$stringArray[$i] = array(
$closeTags . $elem,
true
);
}
}
}
$result = "";
foreach ($stringArray as $stringElem) {
if ($stringElem[1]) {
$result .= $stringElem[0];
}
}
return $result;
}
// Aufruf
$string = "<p><b>Sofort gelöscht</p>";
echo "Bsp.: ".htmlspecialchars($string)."\n";
// Entfernen nicht-geschlossener Tags
echo handle_unclosed_tags($string)."\n";
// Automatisches Schließen nicht-geschlossener Tags
echo handle_unclosed_tags($string, true)."\n";
aber eben auchFalls Übereinstimmungen gefunden wurden, wird die neue subject zurückgegeben, andernfalls wird subject unverändert zurückgegeben
oder NULL, falls ein Fehler auftrat.
// Leere Tags entfernen
function remove_empty_tags ($string, $tags) {
$p_o_tag = '<('.implode('|', $tags).')(?:\s[^>]*[^\/])?';
$p_o_tag_short_tag = '\/>';
$p_o_tag_long_tag = '>';
$p_empty = '(?: |\x00|\xa0|\s)*';
$p_cl_tag = '<\/\\1>';
$pattern =
$p_o_tag
.'(?:'.$p_o_tag_short_tag
.'|'.$p_o_tag_long_tag.$p_empty.$p_cl_tag.')';
while (
$string !=
($val = preg_replace('/'.$pattern.'/iS', '', $string))
&& is_string($val)
) {
$string = $val;
}
return $string;
}
// Aufruf
$string = "<p><p><b><strong> <p></p><p /> </strong></b></p></p>";
$string = "<div>"{$string}"</div>";
$tags = array('p', 'strong', 'b');
echo remove_empty_tags($string, $tags)."\n";