otuatail Posted July 24, 2007 Share Posted July 24, 2007 is there a function to replace a section af a string with another regardless of the values and to do the reverse i.e. $srt1 = "ABCDEFGHIJKLM") $str2 = "XY // insert $str2 in to $str1 [3] and [4] $srt1 = "ABCXYFGHIJKLM") AND $str = $str1 positions [7] and [8] $str = "HI"; I want to do some incripion on text. OR will I have to create something? Desmond. Link to comment https://forums.phpfreaks.com/topic/61538-solved-string-insertion-and-extraction/ Share on other sites More sharing options...
trq Posted July 24, 2007 Share Posted July 24, 2007 This may help as an example... <?php $s = 'abcdefg'; $s[1] = 'x'; $s[2] = 'y'; echo $s; ?> Link to comment https://forums.phpfreaks.com/topic/61538-solved-string-insertion-and-extraction/#findComment-306276 Share on other sites More sharing options...
jd2007 Posted July 24, 2007 Share Posted July 24, 2007 $srt1 = "ABCDEFGHIJKLM"; $str2 = "XY"; $char1=str_split($srt1); $char2=str_split($srt2); $char1[2]=$char2[0]; $char1[3]=$char2[1]; $srt1=join("", $char1); $char3=str_split($srt1); $str=$char3[7].$char3[8]; let me explain the above . u have two strings $srt1 and $str2. u want to place $str2 in $srt1 at positions where C and D is now currently. so, u split $srt1 into an array called $char1. note that the first element starts with 0 not 1, so $char1[0] is A. Now, you split $str2 into an array also called $char2. Now, $char2[0] is X and $char2[1] is Y. now, u want to replace C with $char2[0]. C is $char1[2]. in other words, u want to replace $char2[0] with $char1[2]. So, u assign $char2[0]'s value into $char1[2]. now, u want to replace D with $char2[1]. D is $char1[3]. in other words, u want to replace $char2[1] with $char1[3]. So, u assign $char2[1]'s value into $char1[3]. now, $char1[2] is X and $char1[3] is Y. now, u would need to join all elements to form this string: $srt1 = "ABCXYFGHIJKLM". so, use the join function : join("", $char1);...go here to see info about the join() function...http://www.w3schools.com/php/func_string_join.asp. now, u want H and I from $srt1 and place them in $str like this $str="HI". so, u split $srt1 into $char3...$char3[7] and $char[8] is H and I. u concetenate them in a string $str...check this sites out www.w3schools.com and www.php.net/manual Link to comment https://forums.phpfreaks.com/topic/61538-solved-string-insertion-and-extraction/#findComment-306313 Share on other sites More sharing options...
otuatail Posted July 24, 2007 Author Share Posted July 24, 2007 Ok thanks that works but if I say $line = time(); $line[5] = "0"; Cannot use a scalar value as an array Need to convert this to a standard string. Desmond. Link to comment https://forums.phpfreaks.com/topic/61538-solved-string-insertion-and-extraction/#findComment-306325 Share on other sites More sharing options...
otuatail Posted July 24, 2007 Author Share Posted July 24, 2007 Sorry what thorpe said worked without splitting. A for() loop works but only if It is a string. time(); and rand() don't because they are treated as numbers now not strings. Link to comment https://forums.phpfreaks.com/topic/61538-solved-string-insertion-and-extraction/#findComment-306328 Share on other sites More sharing options...
otuatail Posted July 24, 2007 Author Share Posted July 24, 2007 Thanks Guys This seems to work. $p = time(); $p = "1" . $p // Force it to a string value $p[3] = "5" // works now. Thanks. Link to comment https://forums.phpfreaks.com/topic/61538-solved-string-insertion-and-extraction/#findComment-306340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.