SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Hybrid View

  1. #1
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    is there some function in PHP that does exactly the opposite of htmlspecialchars()?

    In Kev's tutorial, under chapter .. Contetn Formatting, is explained how to define custom "tags" for your cms. In the beginning he uses htmlspecialchars to get rid of those < and > etc.

    But I thought it could be useful if there was a tag
    HTML Code:
     and
    that allows the writer of the article to put in some regular html, that would be parsed as regular html, and not as text.

    But to achieve this, I need to reverse &lt; to < etc. And therefore I want a function like htmlregularchars or so.

    Does that exist?

    Thanks
    Thomas

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On PHP.net, there is this example..
    (I modified it to preserve existing html)
    // $document should contain an HTML document.
    // This will remove HTML tags, javascript sections
    // and white space. It will also convert some
    // common HTML entities to their text equivalent.

    $search = array ("'&(quot|#34);'i",
    "'&(amp|#38);'i",
    "'&(lt|#60);'i",
    "'&(gt|#62);'i",
    "'&(nbsp|#160);'i",
    "'&(iexcl|#161);'i",
    "'&(cent|#162);'i",
    "'&(pound|#163);'i",
    "'&(copy|#169);'i",
    "'&#(\d+);'e"); // evaluate as php

    $replace = array ("\"",
    "&",
    "<",
    ">",
    " ",
    chr(161),
    chr(162),
    chr(163),
    chr(169),
    "chr(\\1)");

    $text = preg_replace ($search, $replace, $document);


  3. #3
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code.

    Is it possible to do it with ereg_replace() instead of preg_replace. What are the differences between the two forms of regular expressions?

    Thomas

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace allows you to use Perl style pattern matching and can take arrays as params.. you cant simply swap one for another.

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just use:

    $text = str_replace ("&lt;", "<", $text);
    $text = str_replace ("&gt;", ">", $text);

    That is if you only need these two characters:

    "<" and ">"

    Hope this helps

  6. #6
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The method I will take depends on which ca-haractars htmlspecialchars() replaces.

    Does someone has a list of that?

    Thanks
    Thomas

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (taken from php.net)

    "The translations performed are:


    '&' (ampersand) becomes '&amp;'

    '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.

    ''' (single quote) becomes '&#039;' only when ENT_QUOTES is set.

    '<' (less than) becomes '&lt;'

    '>' (greater than) becomes '&gt;' "

  8. #8
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thanks.
    That's all I need to know. I will take Pete's way, but with the other translated stuff too.

    I'm not familiar with Perl.

    Thanks everybody!

    Btw, str_replace, is it exactly the same as ereg_replace()?

    Thomas

  9. #9
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Manual:
    This function replaces all occurences of needle in haystack with the given str. If you don't need fancy replacing rules, you should always use this function instead of ereg_replace().
    Why is this? Has it something to do with speed?

    And what is haystack? And needle?
    </thomas>

  10. #10
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ever heard the phrase "needle in a haystack" ?
    It simply means it looks for needle in haystack

    The reason that preg.. is used is because it can accept an array as a parameter.. making it a lot simpler to replace a lot of things in one go.

    If you are looking for speed, use..
    str_replace ("&lt;", "<", str_replace ("&gt;", ">", str_replace ("&amp;", "&", $text) ) );

    etc for each html entity..

  11. #11
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg is faster than ereg from my experience(i talk purely from what i see, never ran benchmarks)

    ---------------------------
    myPHPhost.com

  12. #12
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thanks everyone for helping me.

    Now I'm faced with this:

    There is a variable that contains a bunch of text.
    $text = "blabla";

    That variable is put into htmlspecialchars():
    $text = htmlspecialchars($text);

    In this variable will be special codes like
    HTML Code:
     and
    to indicate to the script that the stuff between those tags are treated as regular html.

    Now I need some code to find those html tags, and then replace the <, >, &, " and ' within those tags.
    How should I do that?

    Thomas

  13. #13
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I 'm not quite sure what you mean but i give it a go:

    $text = str_replace ("&lt;html&gt;", "<html>", $text);
    $text = str_replace ("&lt;/html&gt;", "</html>", $text);

    this replaces the <html> and </html> tags. They must be lower case to change them.
    <Edited by petesmc on 01-15-2001 at 01:54 PM>

  14. #14
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I mean:

    In Kevin Yank's tutorial on PHP/MySql, chapter on Content Formatting, he explaines how to format your text using tags like [ .. ] (for example [ b ]) (I can't put them together, because otherwise the forum would think it's real)

    Now I want to include something like that, but with [html] tags.

    With the bold stuff, it's just [ b ] that is replaced with <B>. But with the HTML tags [html], that is inappropriate.
    The html tags should disappear, and on the contents within it, the next transformations should be made:

    $text = str_replace("&amp;","&",$text);
    $text = str_replace("&lt;","<",$text);
    $text = str_replace("&gt;",">",$text);
    $text = str_replace("&quot;","\"",$text);
    $text = str_replace("&#039;","'",$text);

    How should I do that?
    <Edited by Jppr on 01-15-2001 at 02:17 PM>

  15. #15
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean:

    $text = str_replace("\
    HTML Code:
    ","<html>",$text);
    $text = str_replace("\
    ","</html>",$text);
    $text = str_replace("&amp;","&",$text);
    $text = str_replace("&lt;","<",$text);
    $text = str_replace("&gt;",">",$text);
    $text = str_replace("&quot;","\"",$text);
    $text = str_replace("&#039;","'",$text);

    I'm not sure if i understand...

  16. #16
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, not really.

    First the script should find the portions of the $text var, that are within
    HTML Code:
     and
    tags.
    Then with that portions the transformations should be made, so that &,",',<,> are displayed as should be.
    If I don't, those chars will be treated as regular text, while they should be pure html code.

    See?

  17. #17
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, now i understand but i don't know how to do that.

    i am guessing that you would use an explode function but i'm not the person to ask, maybe Freddy and Chris ( TWTCommish)

    Sorry about that...

  18. #18
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nah, he wants to avoid converting the HTML between
    HTML Code:
     and
    .. I think
    I suppose you will want to avoid regexps again?

    if so,
    e.g.

    $text = "<something>
    HTML Code:
    <b>html here!</b>
    <not here>
    HTML Code:
     <b>and here!</b>
    ";

    $something = explode("
    HTML Code:
    ", $text);
    
    while( list($key, $val) = each($something)) 
    { 
    $somethingelse = explode("
    ", $val);
    $newarr[] = $somethingelse[0] . htmlspecialchars($somethingelse[1]);
    }

    $text = implode("", $newarr);


    How ever, a regexp may in fact be just easier..

  19. #19
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read your post again.. and I realised you may mean something different to what I thought..

    If you want the text between
    HTML Code:
     and
    to be converted to special chars,

    change to this..

    $newarr[] = htmlspecialchars($somethingelse[0]) . $somethingelse[1];

  20. #20
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, do I speak Chinese or what

    No, in the very beginning of the script, the whole $text var will be htmlspecialchars'ed. That's because of my custom tags.

    Now, in one tag (the [html] one) I want to undo the htmlspecialchars(). I want exactly the opposite.

    Normally, that wouldn't be the problem. But it's because these [html] portions are within a large text amount, that I have to use a regexp first to find those portions, and then, in these portions, I need to do the replacement of &amp;, &quot;, &lt;, &gt;, &#034;.

    See what I mean ?

    How should I accomplish such a mix of stuff.

    By the way, what d these implode and explode functions do?

    Thanks a lot for all of your help, especially PeterW and petesmc.

  21. #21
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what my code does

    $text = "
    HTML Code:
    <b>html here!</b>
    <not here>
    HTML Code:
     <b>and here!</b>
    ";

    // take $text as your text..
    // everything

    $something = explode("[html]", $text);
    // explode where this a
    HTML Code:
    while( list($key, $val) = each($something)) 
    {
    // for every block of [html]
    $somethingelse = explode("
    ", $val);
    // explode in to two parts, before and after [/html]
    $newarr[] = $somethingelse[0] . htmlspecialchars($somethingelse[1]);
    // the part outside the you do htmlspecialchars on..
    }

    $text = implode("", $newarr);
    // join them back up...

    This produces...
    <b>html here!</b>&lt;not here&gt; <b>and here!</b>

  22. #22
    SitePoint Zealot Jppr's Avatar
    Join Date
    Dec 2000
    Location
    Belgium
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I didn't tested it yet, but it looks promising!
    Brilliant code PeterW!

    Another question: where should I put the code to convert other codes like [ b ] , and [ code ] and [ link ], etc.?
    I suppose after the implode command, is that correct?

    The code will be something like

    Code:
    $text = ereg_replace("\[ b ]","<B>",$text);
    ...
    $text = ereg_replace("\[ link ]([-_./a-zA-Z0-9!&?%#,'=:~]+)\[/ link ]","<A HREF=\"\\1\">\\1</A>",$text);
    Something else: if I wanted to explain the readers of an article (the $text var) how to use the [html] stuff and other custom codes, I'll have to use these codes. How should I prevent the code parsing it? Just by putting it in [html] portions?

    PeterW, thanks a LOT!

    Cheers
    Thomas


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •