Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    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
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    \{(.*)\}
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #3
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    OR

    \{{2}(.*)\}{2} If there are always going to be double 'curly' brackets.
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #4
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    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

  • #5
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Why re-invent the wheel?

    For the format you suggest, use the PHP parse_ini_file() function PHP: parse_ini_file - Manual and changing your templates to the standard 'ini file' format or use the PHP Smarty Engine PHP Template Engine | Smarty for the template parsing.
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #6
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Yes I had already considered parse_ini_file function but the problem with it is that if there are values with double quotes, it has to be escaped. This is the reason I was avoiding this function.

    Do you think there is a work around for this?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •