SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A regex question

    Okay, I know how to use preg_match to match a pattern within a string. But what I'd like to do is perform character replacements on all of the string EXCEPT the part matched by my regex.

    Basically so I could do "vlahvlddjvj[TAG]Stuff[/TAG]jkdskdjd" and everything outside the [TAG] tags would be processed. I can match the part within the tags, but Ive no idea how to go about making it do what I want.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understnad you correctly, you juts want to delete the stuff on the [tag] tags?

    PHP Code:
    $txt preg_replace('#\[i](.+?)\[/i]#is'''$txt); 
    Should do that.

    Just replace it with nothing.

    If I didn't understnad what you were saying, please completly ignore this post, and explain more.

  3. #3
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really. Basically what Im doing is writing a basic CMS system, all the code within the tags will be left alone when inserted into the database. Wheras everything outside the tags will be de-htmlized.

    This is so for quick and easy entries you can use plain text and have line breaks converted automatically, but you can use tags to include snippets of html without them having their < & > tags converted to &lt; & &gt; .

    It would be a lot easier to do it the other way around, but I'd like to have the auto-convert on by default.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  4. #4
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually: Never mind, I found a way.

    PHP Code:
    $str "Normal <p>text</p>, <b>normal</b> text.[NOBR]<b>bold bit</b>Stuff[/NOBR]More normal text";

    preg_match("#(.*)(\[NOBR\])(.+?)(\[/NOBR\])(.*)#ies"$str$nah);

    $nah[1] = htmlspecialchars($nah[1]);
    $nah[5] = htmlspecialchars($nah[5]); 
    This does what I needed.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't look like that would work with mutiple [NOBR]s. I see the .*s at the end or beginning gobbling them up.

    This may be sufficient to keep that from happening:

    Code:
     preg_match_all("#(.*?)(([NOBR])(.+?)([/NOBR]))?([^\[]*)#ies", $str, $nah);
    My, look at all those question marks. There's probably an easier way but it is not coming to my mind at the moment .
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


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
  •