473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to echo text from character 2-27 in a file

I have a file and I want to echo the text from character 2 to 27. How can I do this?

Thanks
May 14 '10 #1

✓ answered by giorgisp

Oh, I found what I was doing wrong! substr() has parameters on where to search, start and how much characters to return from the start character, not the end character. So the updated code is:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $break = "\n"; //set the line break character
  4. $textfile = "cake"; //set the file we want to read
  5. $file = fopen("$textfile", 'r'); // open the file in read mode
  6. $data = fread($file, filesize($textfile)); // read the whole file and save it in a variable
  7. fclose($file); //close the file
  8.  
  9. $start = strpos($data, $_GET["email"]); //find the first character's position of the string given in the url
  10. $end = strpos($data, $break, $start ); //find the position of the last character in the line
  11.  
  12. $end1 = $end - $start; //subtract the end character from the start character to find the string lenght and save it in a variable
  13.  
  14. echo substr($data, $start, $end1); //use substr to trim the file contents and provide it with the file content, the starting character and the lenght and echo the part/line we want
  15.  
  16. ?>
  17.  
And it works perfectly!

4 2032
dlite922
1,584 Expert 1GB
You can do it with PHP. But I can't just give you the code.

@ bytes we like the user to fully understand the code they are producing.

So I'll point you in the right direction and see if you can write some code. If it doesn't do what you expect or doesn't work, come back here and ask us!

Depending on the size of the file, if small see http://php.net/file, if it's large see http://php.net/fopen , fread, etc.

Then you must have a loop. A while loop or for, foreach loop.

To get the characters you want, use this handy dandy function called sub string: http://us.php.net/substr

I hope that gives you a good start. Let us know!






Dan
May 14 '10 #2
Thanks for the reply. The file has in each line an email address and some text next to it seperated with a space. So I give the email address I want to the script with the get method (url variable) and I want it to echo the email address along with the text next to it (from the start of the line to the end of the line). I just wrote this code to do this but it didn't work as I expected. Sometimes it gives me back more characters than what I want.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $break = "\n";
  4. $textfile = "cake";
  5. $file = fopen("$textfile", 'r');
  6. $data = fread($file, filesize($textfile));
  7. fclose($file);
  8.  
  9. $start = strpos($data, $_GET["email"]);
  10. $end = strpos($data, $break, $start );
  11.  
  12. echo substr($data, $start, $end);
  13.  
  14. ?>
May 14 '10 #3
Oh, I found what I was doing wrong! substr() has parameters on where to search, start and how much characters to return from the start character, not the end character. So the updated code is:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $break = "\n"; //set the line break character
  4. $textfile = "cake"; //set the file we want to read
  5. $file = fopen("$textfile", 'r'); // open the file in read mode
  6. $data = fread($file, filesize($textfile)); // read the whole file and save it in a variable
  7. fclose($file); //close the file
  8.  
  9. $start = strpos($data, $_GET["email"]); //find the first character's position of the string given in the url
  10. $end = strpos($data, $break, $start ); //find the position of the last character in the line
  11.  
  12. $end1 = $end - $start; //subtract the end character from the start character to find the string lenght and save it in a variable
  13.  
  14. echo substr($data, $start, $end1); //use substr to trim the file contents and provide it with the file content, the starting character and the lenght and echo the part/line we want
  15.  
  16. ?>
  17.  
And it works perfectly!
May 14 '10 #4
dlite922
1,584 Expert 1GB
Glad you got it working!






Dan
May 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
6
by: Cyril Vi?ville | last post by:
As it's written in the subject, i would like to know how can i find which program modified a file (last access, last changed). By example: Example.txt in a log directory I need to know which...
2
by: JollyK | last post by:
Hi friends, This is my question.... From the Page Load event (or Page Init event), I would need to find which event had occurred that caused a PostBack, for example was it a event fired from...
22
by: DraguVaso | last post by:
Hi, For my application I need the following behavior: When I press F4 the cursor has to move to the next line in my multiline textbox which begins with "0". Finding lines starting with 0 isn't...
0
by: programmer2004 | last post by:
Hi How to find which windows API(.dll) is getting called when working with some events. for e.g. when click on windows open menu, save menu, print menu. once API knows then how to find all...
5
by: su | last post by:
to find which process dumped core at the promt we give $ file core.28424 core.28424: ELF 32-bit LSB core file of 'soffice.bin' (signal 11), Intel 80386, version 1 (SYSV), from 'soffice.bin' ...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
2
by: Divakar | last post by:
Hi, How can we find which SQL statement is executing when "list application show detail" shows "UOW Executing" against an application. This topic was touched upon earlier by one of the member....
3
by: jeffsteed | last post by:
I am using VisualStudio 2005 to create an application in C#. I have a scrollable, multiline textbox where I want the user to be able to doubleclick any given line to put that line in another...
10
by: Gary | last post by:
I'm trying to find a way to determin which window Within-Another-Program currently has the focus. THE SITUATION: (Skip down to "My Question" if you don't want the background...) * This is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.