SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    grab known part of string

    Got this template that I want to read placeholders from.

    this is the pattern I want to grab
    Code:
     [PLACEHOLDER_NAME:DESCRIPTION]

    Code:
    <html>
    <head>
     [head:the head section of the page]
    </head>
    <body>
     [body:a line with description]
    </body>
    </html>
    Now how do I grab all placeholders in an array?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What about...

    PHP Code:
    <?php
    //--> Get template
    $sTemplate file_get_contents('template.tpl');
    //--> Parse template and get matches
    $aMatches = (preg_match_all('/\\[.*?\\]/'$sTemplate$aMatchesPREG_PATTERN_ORDER)) ? $aMatches[0] : array() ;
    //--> See if we have any matches
    if( count($aMatches) > )
    {
        
    //--> Dump the matches.
        
    print_r($aMatches);
    }
    ?>
    SilverB.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that actually did the trick! thanks!

  4. #4
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what if the placeholder changed from
    Code:
    [PLACEHOLDER_NAME:DESCRIPTION]
    to
    Code:
    [[PLACEHOLDER_NAME:DESCRIPTION]]
    note the extra [ and ]...
    How would I go from there? By just adding an extra [ and ] in the regex it doesn't work

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /\\[\\[.*?\\]\\]/

    this did the trick


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
  •