Solved

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

Posted on 2013-01-02
12
462 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 35

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

679 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