Current location: Hot Scripts Forums » Programming Languages » PHP » Read specific value in text file?


Read specific value in text file?

Reply
  #1 (permalink)  
Old 08-20-06, 05:36 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Read specific value in text file?

greetings,


i have a text file contains following
xyz=abc
assad=asd
wer=sfdd

now i want to read this file and want to get the specific value from it like

i need value of "xyz"

how will read that specific value from text file?

mohit
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-20-06, 09:28 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:



function get_value_of($name)
{
     
$lines file('yourfile.txt');

     foreach (
array_values($lines) AS $line)
     {
          list(
$key$val) = explode('='trim($line) );
          
          if (
trim($key) == $name)
          {
                return 
$val;
          }
     }
     return 
false;

Untested bu should do it. Usage example:
PHP Code:



echo get_value_of('xyz'); 

EDIT:

This should also do it.
PHP Code:

function get_value_of($name)

{
    
$file 'data.txt';

    
preg_match('/'$name .'\s?=\s?([a-z0-9\s_-]*)\r?\n/i'file_get_contents($file), $matches);    
    return 
$matches[1];


Last edited by nico_swd; 08-20-06 at 12:10 PM. Reason: Forgot a semicolon.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-23-06, 08:20 AM
mohit's Avatar
mohit mohit is offline
Newbie Coder
 
Join Date: Jul 2006
Location: India
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks...nico_swd..
it was really very helpful...
Thanks again..
mohit
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Draggable Tables Ares JavaScript 10 08-03-06 07:55 AM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 04:27 PM
how to separate and read data from text file robbydweb PHP 3 12-17-04 03:22 AM
Can javascript be used to read and write to a text file kept on a webserver? gmb1994 JavaScript 4 11-23-04 11:50 AM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 08:19 PM


All times are GMT -5. The time now is 07:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.