Solved

deleting specific line from a text file, using PHP...

Posted on 2013-01-02
12
446 Views
Last Modified: 2013-01-02
Hi, I have  a file as follows:

[general]
srvlookup=yes
register =>17555245531:secret@sip.mysip.com
nat=yes
defaultexpiry=100
qualify=yes
register...
canreinvite=no
bindport=5060
bindaddr=0.0.0.0
autoreload=always


I need to delete the line that has the string 17555245531:secret@sip.mysip.com. How do I do this? When I run the attached code the first line [general] gets deleted.. Thank you for the help.

Open in new window

$txt = file_get_contents($file); 
        $Blocks = explode(PHP_EOL, $txt); 
		$pLine='17555245531:secret@sip.mysip.com'
		$del= array_search("$pLine",$Blocks); 
		unset($Blocks[$del]);

Open in new window

0
Comment
Question by:aej1973
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 38737999
Take the double quotes off of $pLine in your array_search statement.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38738029
It deletes the first line (0 indexed) because array_search returns 0, that is false: it doesn't find the element you're looking for because (I think) you miss a semicolon in your code:

$pLine='17555245531:secret@sip.mysip.com'

instead of

$pLine='17555245531:secret@sip.mysip.com';

Try this while I test your code.

Cheers
0
 

Author Comment

by:aej1973
ID: 38738033
Hello Barry62, there was no change once I removed the double quotes, the line that gets deleted is [general].
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38738043
The problem is that when you explode your text you get an array umber indexed:

    [1] => srvlookup=yes
    [2] => register =>17555245531:secret@sip.mysip.com
    [3] => nat=yes
    [4] => defaultexpiry=100
    [5] => qualify=yes
    [6] => register...
    [7] => canreinvite=no
    [8] => bindport=5060
    [9] => bindaddr=0.0.0.0
    [10] => autoreload=always

So the value you should looking for is

'register =>17555245531:secret@sip.mysip.com'

$txt = file_get_contents($file); 
        $Blocks = explode(PHP_EOL, $txt); 
		$pLine='register =>17555245531:secret@sip.mysip.com'
		$del= array_search("$pLine",$Blocks); 
		unset($Blocks[$del]);

Open in new window

0
 

Author Comment

by:aej1973
ID: 38738064
yes, that is why I was wondering if I could search for a specific string in the line and delete the whole line if the string was found.

Thank you.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38738092
try in_array() instead of array_search().  It uses a loose comparison.  also, MarqusG is correct.  Put a semicolon on the 3rd line of your code.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Accepted Solution

by:
Marco Gasi earned 300 total points
ID: 38738118
Here you go:

$txt = file_get_contents($txt);
$Blocks = explode(PHP_EOL, $txt); 
$pLine='17555245531:secret@sip.mysip.com';
for ($i = 0; $i < count($Blocks); $i++){
  if (stristr($Blocks[$i], $pLine)) $del = $i;
}
unset($Blocks[$del]);

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38738184
If you prefer input array be reindexed you can use array_splice:

$txt = file_get_contents($txt);
$Blocks = explode(PHP_EOL, $txt); 
$pLine='17555245531:secret@sip.mysip.com';
for ($i = 0; $i < count($Blocks); $i++){
  if (stristr($Blocks[$i], $pLine)) $del = $i;
}
array_splice($Blocks, $del, 1);

Open in new window

0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38738193
I would've just said to use a preg_replace:

$txt = file_get_contents($txt);
$txt = preg_replace("/\bregister =>.+[\n\r]+/","",$txt);
0
 

Author Comment

by:aej1973
ID: 38738360
Thank you Marcus, it works great!
0
 

Author Closing Comment

by:aej1973
ID: 38738362
Thank you...
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38738376
Glad to help you! On to the next, aej1973.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now