Jump to content

[SOLVED] String insertion and extraction


otuatail

Recommended Posts

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.