First, let's look at your own writing method.
The code is as follows: |
Copy code |
Str_replace ("<div class = \" summary-text \ ">", '', str_replace ('</div> ','', str_replace ('</div>', '', $ vv ))) |
The simplest method is to replace the two divs specified in the $ vv variable. Later, we found a solution.
The code is as follows: |
Copy code |
$ Info = strip_tags ($ vv ); |
All html tags are replaced. Let's look at the strip_tags function.
Trip_tags (string, allow): The function removes HTML, XML, and PHP labels.
The code is as follows: |
Copy code |
$ Str = 'Guo bowl Basin-<span style = "color: # f00;"> PHP </span> '; $ Str1 = strip_tags ($ str); // delete all HTML tags $ Str2 = strip_tags ($ str, '<span>'); // retain the <span> tag Echo $ str1; // output Guo bowl Basin-PHP Echo $ str2; // different styles |
As we can see above, some html tags can only be retained in PHP, but some html tags cannot be specified to be deleted. So I wrote a file for my usual lib Library.
The code is as follows: |
Copy code |
/** * Remove the specified HTML tag */ Function strip_only_tags ($ str, $ tags, $ stripContent = FALSE ){ $ Content = ''; If (! Is_array ($ tags )){ $ Tags = (strpos ($ str, '> ')! = False? Explode ('>', str_replace ('<', '', $ tags): array ($ tags )); If (end ($ tags) = ''){ Array_pop ($ tags ); } } Foreach ($ tags as $ tag ){ If ($ stripContent ){ $ Content = '(. + <! -- '. $ Tag.' (--> | \ s [^>] *>) | )'; } $ Str = preg_replace ('# <! --? '. $ Tag.' (--> | \ s [^>] *>) '. $ content.' # is ', '', $ str ); } Return $ str; }
|
Parameter description
$ Str-refers to a string to be filtered, such as div, p, em, img, and other html tags.
$ Tags-indicates that you want to remove a specified html tag, such as a, img, and p.
$ StripContent = FALSE-remove the content in the tag, such as deleting the entire link. The default value is False, that is, do not delete the content in the tag.
Instructions for Use
The code is as follows: |
Copy code |
$ Target = strip_only_tags ($ source, array ('A', 'em ',' B ')); |
Removes tags a, em, and B from the $ source string.