SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stripping out with preg_replace ??

    This may have been covered already but I can't find it. I know how this function works and have used it, but for some reason can't get this to work. I'm trying to strip out special tags from MS Word out of a copy and paste into a form when posting. These tags look like <!-- /*MS Word code*/ <span.........<font mso etc -->. I want to remove the whole html tag, everything inbetween <!-- --> and including the html tag. This is what I came up and have tried with multiple variances but does not seem to work
    PHP Code:
    @preg_replace("/<!--[.*]-->/"""$var['formpost']); 

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace() returns the new, modified string. You need to capture this string into a variable so you can use it.

    the . meta character does not match new line characters by defualt. Use the s flag if you want it to.

    .* is greedy. Greedy means it will try to match as much as it possibly can. This could end up matching much more than you want.

    .*? is not greedy. It matches as little as possible. This is probably what you want.

    the @ operator silences php errors. This makes it very hard for you to see problems with your code. You generally shouldn't use it, save for a few rare cases where an it's not possible to avoid an error. This is not one of them. If you're getting errors, they need to be fixed. Silencing the error doesn't make your code magically start working.

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was in a variable but just had to add some flags and change the brackets....Doh! I don't know if I need it to be greedy or not, we'll find out in the long run lol thanks for the pointer tho!
    PHP Code:
    $var['formpost'] = preg_replace("/<!--(.*)-->/si"""$var['formpost']); 


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
  •