SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast mohammedsk's Avatar
    Join Date
    Nov 2005
    Location
    Columbia, Missouri
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    parse_ini_file and new line character

    Hi,
    I came across the PHP function parse_ini_file(), which I found very useful for my application. It works fine, but I have small problem, I am not sure how to go about.

    Sample input file to PHP script:
    Code:
    [journal]
    id = "12575"
    title = This is the title
    abstract = This is the abstract
    source = This is the sourcedfgdfg
    
    [journal2]
    id = "1wd5442575"
    title = This is the title2
    abstract = This is the abstract2
    source = This is the source2fghfghs
    This line belongs to source, but it get ignored.
    PHP script:
    PHP Code:
    $ini_array parse_ini_file("data_file.txt"true);

    foreach(
    $ini_array as $key=>$val){
        foreach(
    $val as $key1=>$val){
            echo 
    "$key1:$val<br>";
        }
        echo 
    "<br>****<br>";

    Sample output:
    Code:
    id:12575
    title:This is the title
    abstract:This is the abstract
    source:This is the sourcedfgdfg
    
    ****
    id:1wd5442575
    title:This is the title2
    abstract:This is the abstract2
    source:This is the source2fghfghs
    
    ****
    As you may notice the very last line "This line belongs to source, but it get ignored." got ignored and not printed out on the screen.

    Any ideas how to deal with new line character?
    Any help is appreciated
    Thanks,

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Enclose the entire string in double quotes.

    Code:
    [journal]
    id = "12575"
    title = This is the title
    abstract = This is the abstract
    source = This is the sourcedfgdfg
    
    [journal2]
    id = "1wd5442575"
    title = This is the title2
    abstract = This is the abstract2
    source = "This is the source2fghfghs
    This line belongs to source, but it get ignored."

  3. #3
    SitePoint Enthusiast mohammedsk's Avatar
    Join Date
    Nov 2005
    Location
    Columbia, Missouri
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot,
    I have another question regrading regular expression.
    I want to replace all occurrences of the following with nothing in a text:
    !@#$%^&*()-=_+[]{}, etc
    I know I need to use preg_replace('/[._-]+/','', $text).
    I am just not sure how to escape some characters like the dollar sign.
    I want to put all in one regular expression.


    Thanks for any help.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $txt 
    "id = \"1wd5442575\"\n".
            
    "title = This is the title2\n".
            
    "abstract = This is the abstract2\n".
            
    "source = This is the source2fghfghs\n".
            
    "This line belongs to source, but it get ignored.\n";

    function 
    myparse_ini_file$txt_to_parse ) {
        
    $out = array();
        
    $parts explode"\n"$txt_to_parse );
        foreach( 
    $parts as $part ) {
            
    $tmp_val "";
            if ( 
    strpos$part"=" ) ) {
                list(
    $key$value ) = explode"="$part );
                
    $out[$key] = $value;
            } else {
                
    end$out );
                
    $key key($out);
                
    $out[$key] .= " "$part;
            }
        }
        return 
    $out;
    }

    var_dumpmyparse_ini_file$txt ));
    ?>
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


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
  •