Solved

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

Posted on 2013-01-02
12
444 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 30

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 30

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 30

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 30

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 30

Expert Comment

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

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

707 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

18 Experts available now in Live!

Get 1:1 Help Now