Deleting certain lines?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ozzieing
Forum Newbie
Posts: 3
Joined: Tue Jul 07, 2009 6:29 am

Deleting certain lines?

Post by ozzieing »

Currently my PHP writes out to an XML document via POST in this kind of format.

Code: Select all

<key>User1</key> 
<string>George</string>
<key>User2</key>
<string></string> 
<key>User3</key>
<string>Ryan</string>
But I want to remove the users at certain points. So if George is sent to "Remove.php" via POST then "George" is removed so it then looks like:

Code: Select all

<key>User1</key> 
<string></string>
<key>User2</key>
<string></string> 
<key>User3</key>
<string>Ryan</string>
Any help with this? It should be fairly basic but I have no idea where to start. To be honest, I'm a bit of a PHP noob but once this is sorted my application wont need any more PHP.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Deleting certain lines?

Post by jackpf »

You should probably use DOM, but I don't know much about it myself as I've never needed to use it.

However, you could do something like this

Code: Select all

$file = file_get_contents('xml.xml');
$file = explode("\n", $file);
 
$name = 'George';
 
foreach($file as $key => $value)
{
    if(strstr($value, '<string>'.$name.'</string>'))
        unset($file[$key]);
}
 
echo implode("\n", $file);
Although that's a pretty dodgy way of doing it. It may be in your interests to just learn DOM.
ozzieing
Forum Newbie
Posts: 3
Joined: Tue Jul 07, 2009 6:29 am

Re: Deleting certain lines?

Post by ozzieing »

jackpf wrote:You should probably use DOM, but I don't know much about it myself as I've never needed to use it.

However, you could do something like this

Code: Select all

$file = file_get_contents('xml.xml');
$file = explode("\n", $file);
 
$name = 'George';
 
foreach($file as $key => $value)
{
    if(strstr($value, '<string>'.$name.'</string>'))
        unset($file[$key]);
}
 
echo implode("\n", $file);
Although that's a pretty dodgy way of doing it. It may be in your interests to just learn DOM.
Thanks very much! Unfortunately I can't seem to get it to work unless I'm missing something completely. I would learn "DOM" but this is the only time I'm ever going to use this or PHP in general really. Is there something that I could have missed out?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Deleting certain lines?

Post by jackpf »

Hmm...well, I tested it with the xml you provided, and it worked.

What's your code?
ozzieing
Forum Newbie
Posts: 3
Joined: Tue Jul 07, 2009 6:29 am

Re: Deleting certain lines?

Post by ozzieing »

Ah sorry, I've been an idiot. Its not technically an "XML" its a plist which is a type of XML. This means at the top and a few times in the document it has:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
It doesn't understand that code when it checks it which is why it doesn't work. Sorry my mistake, is there any way around this? So like to say check it unless text = that.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Deleting certain lines?

Post by jackpf »

What's the exact file you're trying to parse? I'll have another go at it...but I need to know what I'm trying to find in order to find it.

But it should still work anyway...all that code does is loop through each line, and delete the line if it includes <string>George</string>. So any line containing that should be removed.

Is that not the case?
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: Deleting certain lines?

Post by SvanteH »

Perhaps you forgot that you have to save the file afterwards inorder for the xml to show any effects?
Post Reply