Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
change value inside of string
Old 07-24-2009, 03:53 AM change value inside of string
Experienced Talker

Posts: 41
Trades: 0
is it possible to change a value in the middle of a string
im using fwrite, and i can image a way by storeing each line of the file then expolding the line that my target is on, changing its string then rebuilding that line then putting all the lines back in there place. is there better way to do this however,
flatrat is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-24-2009, 04:07 AM Re: change value inside of string
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
As always, a more in-depth and specific explanation will yield a better answer. When you say change a value in the middle of a string what exactly do you mean? Can you provide a specific example? What is your "target" and how do you need to change it?

PHP provides a lot of functions for processing strings ( http://us2.php.net/manual/en/ref.strings.php ). In addition to this you can traverse a string the same way you can traverse an array (because strings are arrays).

To answer your question, yes you can change a value in the middle of a string.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-24-2009, 04:46 AM Re: change value inside of string
Experienced Talker

Posts: 41
Trades: 0
what i mean is,

can i use fwrite without having to rewrite the entire file if i only want to change something small

sounds kinda silly when i put it like that,

Last edited by flatrat; 07-24-2009 at 04:48 AM..
flatrat is offline
Reply With Quote
View Public Profile
 
Old 07-24-2009, 12:33 PM Re: change value inside of string
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Ok now I see what you're trying to do.

Technically this is possible to do, but it is not as simple as it may appear. If you use fopen in r+ mode to open the file, the file pointer will be placed at the beginning of the file. You can then use fseek to position the file pointer wherever you would like. Think of it like using the arrow keys to move the cursor in a text editor. The only problem is, anything you write to the file will overwrite any existing bytes. For example, assume I have a text file called test.txt with contents "hello world":

PHP Code:
$fh fopen('text.txt''r+');
fseek($fh2); //advance the pointer to the third byte
fwrite($fh'foo'); 
After the above code is execute the contents of the file will look like this:

"hefoo world"

This is exactly what would happen if when editing a text file you hit the insert key and began typing in the middle of the file.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to change value inside of string
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16337 seconds with 12 queries