pastet89 Posted January 16, 2009 Share Posted January 16, 2009 Hi all! I really need some help :queasy: I have these two things: $string = "blabla<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>"; $array = array("b1", "b2", "b3"); What am I trying to do with the code , is to insert in every value='' the appropriate value for it. That is the final string I need: $final_string = "blabla<input type='checkbox' value='b1' name='utakmica'>blaablaablaa<input type='checkbox' value='b2' name='utakmica'>blaablaablaa<input type='checkbox' value='b3' name='utakmica'>"; I tried the code, which worked if the value of the string was "ac ac ac", and instead of $array_string = explode("' name='utakmica'",$string); I used $array_string = explode(" ",$string); . But when I inserted the actual values I need: $array = array("b1", "b2", "b3"); $arraysize = sizeof($array); $string = "blabla<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>"; $array_string = explode("' name='utakmica'",$string); for ($i = 0; $i<$arraysize; $i++) { $combine_array = "". $array[$i] ."". $array_string[$i] .""; $final_string = "$final_string ". $combine_array .""; } echo $final_string; what I get was: b1blabla<input type='checkbox' value=' b2>blaablaablaa<input type='checkbox' value=' b3>blaablaablaa<input type='checkbox' value=' Why is this error happening when I replace "ac ac ac" with more difficult string...? Do you know how can I get the desired stirng: $final_string = "blabla<input type='checkbox' value='b1' name='utakmica'>blaablaablaa<input type='checkbox' value='b2' name='utakmica'>blaablaablaa<input type='checkbox' value='b3' name='utakmica'>"; , using just these two things: $string = "blabla<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>"; $array = array("b1", "b2", "b3"); Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/ Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 tho i dont get why u r doing this, but why don't use str_replace? $string = "blabla<input type='checkbox' value='chk1' name='utakmica'>blaablaablaa<input type='checkbox' value='chk2' name='utakmica'>blaablaablaa<input type='checkbox' value='chk3' name='utakmica'>"; $myarray = array("b1", "b2", "b3"); $srch_array= array("chk1", "chk2", "chk3"); $string=str_replace($srch_array,$srch_array,$string); Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738354 Share on other sites More sharing options...
Zephyr_Pure Posted January 16, 2009 Share Posted January 16, 2009 Well, you could "cheat" and use preg_replace with a limit: $string = "blabla<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>"; $array = array("b1", "b2", "b3"); $count = count($array); for ($x = 0; $x < $count; $x++) { preg_replace("/value=''/","value='".$array[$x]."'",$string,1); } Probably not the most efficient way, but it should work. Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738358 Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 Well, you could "cheat" and use preg_replace with a limit: $string = "blabla<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>blaablaablaa<input type='checkbox' value='' name='utakmica'>"; $array = array("b1", "b2", "b3"); $count = count($array); for ($x = 0; $x < $count; $x++) { preg_replace("/value=''/","value='".$array[$x]."'",$string,1); } Probably not the most efficient way, but it should work. one small correction ... for ($x = 0; $x < $count; $x++) { $string = preg_replace("/value=''/","value='".$array[$x]."'",$string,1); } Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738373 Share on other sites More sharing options...
Zephyr_Pure Posted January 16, 2009 Share Posted January 16, 2009 one small correction ... for ($x = 0; $x < $count; $x++) { $string = preg_replace("/value=''/","value='".$array[$x]."'",$string,1); } Ahh, good catch! Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738376 Share on other sites More sharing options...
Mark Baker Posted January 16, 2009 Share Posted January 16, 2009 Use %s in your base string, then vsprintf() $string = "blabla<input type='checkbox' value='%s' name='utakmica'>blaablaablaa<input type='checkbox' value='%s' name='utakmica'>blaablaablaa<input type='checkbox' value='%s' name='utakmica'>"; $myarray = array("b1", "b2", "b3"); $newString = vsprintf($string,$myarray); Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738384 Share on other sites More sharing options...
pastet89 Posted January 16, 2009 Author Share Posted January 16, 2009 addbuet, Thanks a lot, it worked! Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738387 Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 @Baker ... this is cool .... Quote Link to comment https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/#findComment-738388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.