1

How to delete tag html on php

my data:

$a="<div style='color: blue;'>Hallo</div>
<div>yas!</div><img src='blabla/aa/img.png'> im fine
<span> yes</span> <a href='aaa.com'>link</a>";

How to delete all div and span

<div style='color: blue;'></div>, <div></div> and <span></span>

and not delete

<img src='blabla/aa/img.png'> and <a href='aaa.com'>link</a>

Please help me

5 Answers5

2

How about using strip_tags:

$a="<div style='color: blue;'>Hallo</div>"
   ."<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";

$clean_html = strip_tags($a, "<img><a>");
vstm
  • 12,407
  • 1
  • 51
  • 47
  • @RepbAdhyMusaad: The second parameter of `strip_tags` is called `allowable_tags` and there you can list all the tags you *want to include* in your output. – vstm Sep 05 '14 at 06:04
  • i think this will still leave the " im fine" text (as it's not contained within a tag) wont it? – haxxxton Sep 05 '14 at 06:09
  • @haxxxton: correct, it will remove the `` tag but not it's contents. So for example `strip_tags("hello");` will just return `"hello"`. – vstm Sep 05 '14 at 06:14
  • @vstm, ok, sorry just re-read OP's question, i read it as only return the allowed tags and their data, not to just remove the HTML tags.. +1 – haxxxton Sep 05 '14 at 06:16
2

If you have a white list of element then you can use the strip_tags() function http://php.net/manual/en/function.strip-tags.php.

Another option is to use http://htmlpurifier.org

WhiteList

http://htmlpurifier.org/live/configdoc/plain.html#HTML.AllowedElements

Blacklist

http://htmlpurifier.org/live/configdoc/plain.html#HTML.ForbiddenElements

zakaria
  • 426
  • 2
  • 15
0

Your sentence cant explain what is your goal. Have a look at this.

substr — Return part of a string

Description

string substr ( string $string , int $start [, int $length ] )

Returns the portion of string specified by the start and length parameters.

Parameters

string

The input string. Must be one character or longer.

start

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

If string is less than or equal to start characters long, FALSE will be returned.

Examples

Example - Basic substr() usage

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0];                 // a
echo $string[3];                 // d
echo $string[strlen($string)-1]; // f

?>

Or

If you want to remove HTML tags and only allow some of them you can use strip_tags function like this

 $a="<div style='color: blue;'>Hallo</div>"."<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";

$clean_html = strip_tags($a, "");

This will return the text you entered $text with no tags except
& tags You can read more about strip_tags

Source : http://us2.php.net/manual/en/function.substr.php

akshaivk
  • 427
  • 5
  • 24
0

You Can use this:

 <?php 
$a="<div style='color: blue;'>Hallo</div><div>yas!</div><img src='blabla/aa/img.png'> im fine<span>   yes</span>";
echo strip_tags($a,"<img><a>");
?>
anik4e
  • 493
  • 8
  • 16
0

http://php.net/manual/en/function.preg-replace.php

$a="<div style='color: blue;'>Hallo</div>
<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";

$find=[
    "/<div style='color: blue;'>.*.<\/div>/",
    "/<div>.*.<\/div>/",
    "/<span>.*.<\/span>/"
];

echo preg_replace($find,"",$a);
jam3
  • 129
  • 5