preg_replace(not all tags)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JaNE

    preg_replace(not all tags)

    I need to replace all possible html tags in user input but don't want to
    replace bold and italic start and end tags, so I made folowing line, but
    it replaces only "one-letter-tags" like paragraf or underline tag, it
    doesn't replace body, div, span tags...
    Can anyone help me with this?

    $txt = preg_replace('/<\/?[^bi]>/i', ' ', $txt);

    and problem more: I would like to keep <br> tag and would like to change
    <p> to <br> (and discard </p>)... and... that's it.

    maybe:
    (just as idea, those |?\/... are very confusing to me. I'm still novice
    in php)

    no_html ($txt) {
    $input = array ("'<p>tag'",
    "'<br>,<b>,<i>, </b>,</i>tags"
    "'<any_other>ta g'");

    $output = array ("'<br>'"
    "'<br>,<b>,<i>, </b>,</i>tags'");
    "' '");

    $txt = preg_replace ($input, $output, $txt);
    }

    --
    Jan ko?
    fotografija = zapisano svjetlom | fotozine = foto-e-zin

    --
  • porneL

    #2
    Re: preg_replace(no t all tags)


    strip_tags($inp ut,"<i><b>");


    --
    * html {redirect-to: url(http://browsehappy.pl) ;}

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: preg_replace(no t all tags)

      porneL wrote:[color=blue]
      >
      > strip_tags($inp ut,"<i><b>");
      >
      >[/color]
      Be careful with this though. Allowing <b> also means allowing this:
      <b STYLE="font: 72pt Times New Roman">TEXT HERE</b>

      (Example by Paul Hudson in his excellent work: Practical PHP Programming
      at www.hudzila.org)

      You may want to customize or create a wrapper around strip_tags when
      used with exceptions.

      Schraalhans

      Comment

      • steve

        #4
        Re: Re: preg_replace(no t all tags)

        "pornel" wrote:[color=blue]
        > strip_tags($inp ut,"<i><b>");
        >
        >
        > --
        > * html {redirect-to: url(http://browsehappy.pl) ;}[/color]

        Besides that, when I want to deal with exceptions like this, I usually
        change the exceptions (using str_replace) to something else that regex
        won’t catch, run the regex, and change them back. This may be
        inefficient at times (depending) but surely it is a lot less taxing on
        my brain cells.

        --
        Posted using the http://www.dbforumz.com interface, at author's request
        Articles individually checked for conformance to usenet standards
        Topic URL: http://www.dbforumz.com/PHP-preg_rep...ict188375.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=639933

        Comment

        Working...