Solved

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

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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