SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question Regular Expression

    Hello

    Can someone please tell me the regex that would extract the word hello from the following string:

    Code:
    {{hello}}

    or uk_london from the following string.
    Code:
    {{uk_London}}

    Thanks in advance

  2. #2
    SitePoint Addict bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    278
    Mentioned
    8 Post(s)
    Tagged
    1 Thread(s)
    Is it always {{something}}?

    if that's the case then:
    Code:
    {{(.*)}}
    Period . is a wildcard. Star * means 0 or many. So, {{0 or many of anything other than }}. If you want at least one, use + instead of *.
    Parenthesis is just a logical part, it's accessed via $1 in many cases. You can do each if you wanted to ({{)(.*)(}}) and access each piece respectively $1, $2, $3.

    If you want just letters:
    Code:
    {{([a-zA-Z]*)}}
    or letters and numbers
    Code:
    {{([a-zA-Z\d]*)}}

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    First of all many thanks for the help. It works like a charm. Here is what I wanted to do and have achieved using the regex. All I need is your suggestion if this is the correct way of doing it.


    I want to read a text file (a template as i call it) that can have many sections, each sections are to be separated by this special chars

    Code:
    [++++++++++++++++++++++++++++++]
    and the title of each section will be inside this special curly braces

    Code:
    {{title here}}
    each section can have as many text as required.

    Ex:

    template.txt
    Code:
    [++++++++++++++++++++++++++++++]
    {{First}}
    
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    
    
    
    [++++++++++++++++++++++++++++++]
    {{Second}}
    
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

    Using PHP, I want to read the template.txt file and create an array where the key of the array will be the title and the value will be the content.

    For eg:
    Code:
    Array
    (
        [First] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    
        [Second] => It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    )
    index.php
    PHP Code:
    <?php
    $content 
    file_get_contents('template.txt');
    $explode explode('[++++++++++++++++++++++++++++++]'trim($content));
    $final = array();

    foreach(
    $explode as $key => $val){
        
    preg_match('/{{(.+)}}/'$val$matches);
        
    $variable $matches[0];
        
    $title $matches[1];
        
    $final[$title] = trim(str_replace($variable''$val));
    }

    array_shift($final);
    print_r($final);
    ?>

    Kindly go through the code and let me know / suggest me if this is fine or can be further optimized or made simple??


    Many thanks in advance

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You should use (.+?) to make it non greedy and prevent that in strings like

    {{ replaceme }} and {{ measwell }}

    The expression matches

    "replaceme }} and {{ measwell"

    Instead of

    "replaceme" and "measwell"
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •