To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 03-13-2007, 12:47 PM   #1
essexboyracer
Member
 
Join Date: Jun 2001
Location: UK
Posts: 51
parse_ini_file & duplicate sections

Hi all,

I am trying to use parse_ini_file on some text data that has duplicate section headers in (of varying quantities).

Is there some snazzy function that will scan the ini file, pick up on section headers that are the same and rename them with an increment or something so parse_ini_file wont drop any data from duplicate sections?
essexboyracer is offline   Reply With Quote
Old 03-13-2007, 05:05 PM   #2
sneakyimp
Senior Member
 
Join Date: Apr 2003
Location: LA
Posts: 3,720
rtfm: http://us2.php.net/parse_ini_file

have u tried setting the 2nd arg to TRUE?
__________________
Write multiplayer games in AS3/PHP5 with FlashMOG 0.3.1
Find out your worth with the MyPlan Salary Calculator
sneakyimp is offline   Reply With Quote
Old 03-13-2007, 05:36 PM   #3
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
Quote:
Originally Posted by sneakyimp
rtfm: http://us2.php.net/parse_ini_file

have u tried setting the 2nd arg to TRUE?
I suspect that would have no impact, as the OP stated that there were duplicate section headers, not duplicate item IDs.

As far as a function to rename sections, I don't know of one off-hand. I'd be more inclined to do some sort of search for duplicates and just raise an error if they are found, rather than arbitrarily trying to fix what may be invalid data that should be manually corrected. You could do a preg_match_all('/\[[^\]]+\]/', $text, $matches);, and then see if count($matches) == count(array_unique($matches)).
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Old 03-14-2007, 05:18 AM   #4
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 20,034
As a variation on NogDog's post, assuming that the ini file is stored as an array (one element per line, as per the file() function), then using that regexp in a call to preg_grep() will get you, (a) a list of the section headers, and (b) the lines on which they occur. This information may be useful to you when it comes to further processing.

Hm. Actually if the lines are separated then the regexp might like to be anchored at its ends. Instead of /..../ on the ends squeeze in a ^ and $ to make /^...$/.
__________________
THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER

Last edited by Weedpacket; 03-14-2007 at 05:23 AM.
Weedpacket is offline   Reply With Quote
Old 03-14-2007, 07:51 AM   #5
essexboyracer
Member
 
Join Date: Jun 2001
Location: UK
Posts: 51
thanks guys for your replies, finally sussed it with

Code:
    $subStringCount = substr_count($text, "PROPOSER ACCIDENTS");

	for ($i = 0; $i < $subStringCount; $i++){
        $text = preg_replace("/PROPOSER ACCIDENTS/", "[PROPOSER ACCIDENT " . $i . "]", $text, 1);
    }
I couldnt dump the duplicate section headers as they contained different information within, so i needed a way of renaming the section header so parse_ini would keep them in.

Thank you again!
essexboyracer is offline   Reply With Quote
Old 03-15-2007, 03:20 AM   #6
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 20,034
If you're just looking for the string "PROPOSER ACCIDENTS" then preg_replace is overkill; str_replace() would be much more efficient.
__________________
THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
Weedpacket is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 07:45 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.