RE: [PHP] Re: htmlspecialchars() or htmlentities without destroying tags?
| From: | Timothy Hitchens \(HiTCHO\) | Date: | Wed, 08 Jan 2003 04:44:31 +0000 |
| Subject: | RE: [PHP] Re: htmlspecialchars() or htmlentities without destroying tags? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-130698@lists.php.net to get a copy of this message | ||
Is that going to take care of Nested Tags??
Timothy Hitchens (HiTCHO)
Open Platform Consulting
e-mail: tim@hitcho.com.au
> -----Original Message-----
> From: Greg Beaver [mailto:greg@chiaraquartet.net]
> Sent: Wednesday, 8 January 2003 3:06 PM
> To: php-general@lists.php.net
> Subject: [PHP] Re: htmlspecialchars() or htmlentities without
> destroying tags?
>
>
> Hi Justin,
>
> You have to specify the list of tags you want preserved in an
> array, but this function from phpDocumentor will do it:
>
> /**
> * smart htmlentities, doesn't entity the allowed tags list
> * Since version 1.1, this function uses htmlspecialchars
> instead of htmlentities, for international support
> * @param string $s
> * @return string browser-displayable page
> */
> function adv_htmlentities($s,$html_allowed)
> {
> $___htmltemp = array_flip($html_allowed);
> $___html1 = array();
> foreach($___htmltemp as $tag => $trans)
> {
> $___html1['<'.$tag.'>'] =
> htmlentities('<'.$tag.'>');
> $___html1['</'.$tag.'>'] =
> htmlentities('</'.$tag.'>');
> }
> $_html = array_flip($___html1);
> }
> $result = htmlspecialchars($s);
> $entities =
> array_flip(get_html_translation_table(HTML_SPECIALCHARS));
> $result = strtr($result,$_html);
> $matches = array();
> preg_match_all('/(<img.*>)/U',$result,$matches);
> for($i=0;$i<count($matches[1]);$i++)
> {
> $result =
> str_replace($matches[1][$i],strtr($matches[1][$i],array_flip(g
> et_html_transl
> ation_table(HTML_SPECIALCHARS))),$result);
> }
> preg_match_all('/(<font.*>)/U',$result,$matches);
> for($i=0;$i<count($matches[1]);$i++)
> {
> $result =
> str_replace($matches[1][$i],strtr($matches[1][$i],array_flip(g
> et_html_transl
> ation_table(HTML_SPECIALCHARS))),$result);
> }
> preg_match_all('/(<ol.*>)/U',$result,$matches);
> for($i=0;$i<count($matches[1]);$i++)
> {
> $result =
> str_replace($matches[1][$i],strtr($matches[1][$i],array_flip(g
> et_html_transl
> ation_table(HTML_SPECIALCHARS))),$result);
> }
> preg_match_all('/(<ul.*>)/U',$result,$matches);
> for($i=0;$i<count($matches[1]);$i++)
> {
> $result =
> str_replace($matches[1][$i],strtr($matches[1][$i],array_flip(g
> et_html_transl
> ation_table(HTML_SPECIALCHARS))),$result);
> }
> preg_match_all('/(<li.*>)/U',$result,$matches);
> for($i=0;$i<count($matches[1]);$i++)
> {
> $result =
> str_replace($matches[1][$i],strtr($matches[1][$i],array_flip(g
> et_html_transl
> ation_table(HTML_SPECIALCHARS))),$result);
> }
> preg_match_all('/(<a .*>)/U',$result,$matches);
> for($i=0;$i<count($matches[1]);$i++)
> {
> $result =
> str_replace($matches[1][$i],strtr($matches[1][$i],array_flip(g
> et_html_transl
> ation_table(HTML_SPECIALCHARS))),$result);
> }
> return $result;
> }
>
> Regards,
> Greg
> --
> phpDocumentor
> http://www.phpdoc.org
> "Justin French" <justin@indent.com.au> wrote in message
news:BA41E11F.1BC59%justin@indent.com.au...
> Hi all,
>
> Has anyone got a function or regexp floating around that will convert
behave
> like htmlspecialchars() or htmlentities() (eg converting & to &, "
> to "e;, etc etc) BUT NOT DESTROY HTML TAGS?
>
> Eg:
>
> "hello" said <a href="fred.php">Fred</a> & Judy
>
> should become:
>
> "hello" said <a href="fred.php">Fred & Judy
>
> NOT:
> "hello" said <a href="fred.php">Fred
> &
> Judy
>
> ????
>
> I guess it's stripped down or limited version of htmlspecialchars() or
> htmlentities().
>
>
> Justin
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php