FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


replace a string for a position of text





Mgccl
replace a string only when the 1st piece of string are in the x position

Here I try to make an editor that replace a string in particular position of the file.

Like
$file = FFFFFFF
$string = 'FFFF';
$replace = '0000';
$position = '2';
so what I want is some kind of function
that search for $string and replace with $replace but only if the string's 1st(or nore than one) letter are in $position of the $file....
so, after replace, it will be
F0000FF

any one show me how to do that? because I don't know how to use any expression to use the $position var
hexkid
str_replace() and substr() might do it.

Code:
echo str_replace($string, $replace, substr($file, $position), 1);



EDIT:
It appears your $position is 1-based (the first character in a string is at position 1) while I assumed it was 0-based.
You might want to validate $position (it's always a good thing to do, no matter if you use 1-based or 0-based strings) and substr($file, $position - 1) instead.
edgarinvillegas
Your could do it with str_replace()
If you're using Mysql, you can use the REPLACE function

The syntax of str_replace is:
string str_replace(haystack, new_needle, needle);
Related topics
[TUTOR] A Short Intro to GD
Windows Tips&tricks!
The worlds shortest and laziest Mambo tutorial.
writing COM components in Perl
UBB Code / Smiley Parsing
How to make javascript or dhtml menu
Javascript loops
AJAX tutorial [2nd part now updated]
"if" statements
Who do you think will be the new captain of England team?
Please help images again.
Templating
Windows shortcuts, must know
Replacing a string of text with str_replace
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.