Solved

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

Posted on 2013-01-02
12
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to dynamically set the form action using jQuery.

751 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