Page 1 of 1
Deleting certain lines?
Posted: Tue Jul 07, 2009 6:35 am
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.
Re: Deleting certain lines?
Posted: Tue Jul 07, 2009 7:10 am
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.
Re: Deleting certain lines?
Posted: Tue Jul 07, 2009 8:53 am
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?
Re: Deleting certain lines?
Posted: Tue Jul 07, 2009 10:05 am
by jackpf
Hmm...well, I tested it with the xml you provided, and it worked.
What's your code?
Re: Deleting certain lines?
Posted: Tue Jul 07, 2009 1:18 pm
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.
Re: Deleting certain lines?
Posted: Wed Jul 08, 2009 7:50 am
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?
Re: Deleting certain lines?
Posted: Wed Jul 08, 2009 8:04 am
by SvanteH
Perhaps you forgot that you have to save the file afterwards inorder for the xml to show any effects?