SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex For replacing [intro]

    Im tring to use this:

    PHP Code:
    function prep_data($string) {

            
    $intro_string preg_replace ('/(.*)?\\[intro\\]/i'''$string);
            
    $intro_string preg_replace ('/\\[\/intro\\](.*)/i'""$intro_string);
            
    // now finish off $string
            
    $string preg_replace('/\\[intro](.*)?\\[\/intro\\]/i'""$string);
            
    // run through text->links function
            //$intro_string = $this->textlinks($intro_string);
            //$string = $this->textlinks($string);
            
    $fd = array("intro" => $intro_string"body" => $string);

            return 
    $fd;
        } 
    What it's supposed to do, is take something like

    [intro]
    This is intro!
    [/intro]
    This is the body

    And seperate it into 2 strings, one being "This is intro!" and the other being "This is the body"

    Now, it only works if it is like this:

    [intro]This is intro![/intro]This is the body

    So what am I doing wrong? (this is my first self written regex, if it's even called that)
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  2. #2
    SitePoint Addict DevilBear's Avatar
    Join Date
    Oct 2001
    Location
    Hades
    Posts
    301
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    . doesn't match a newline character. Try the /s modifier; in Perl this is supposed to make the regexp treat the multi-line text as one line. For example you'll have /(.*)?\[intro\]/is for a pattern.

  3. #3
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked, but now it's leaving a \n after each split, i.e. the intro text starts with \n and the body text starts with \n
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  4. #4
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *IDIOT ME POSTED TWICE*
    Last edited by Zaire; Mar 17, 2002 at 21:45.
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  5. #5
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just do a str_replace. It is cheaper than *reg_replace. Of course, that would mean that [intro] without a closing intro could mess up page formatting, perhaps you could add to a stack every time a [intro] is replaced and then pop it when you reach a closing intro [/intro].

  6. #6
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it working, but now I still have this \n after I split it, which doesn't really need to be there.. or does it?
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  7. #7
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Zaire
    I got it working, but now I still have this \n after I split it, which doesn't really need to be there.. or does it?
    I swear, sometimes the biggest moron is actually yourself..

    I totaly forgot about trim() which solved the last of my problems,

    Thanks for all the help
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us


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
  •