Jump to content

Recommended Posts

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");

 

Link to comment
https://forums.phpfreaks.com/topic/141072-a-difficult-replacement-task/
Share on other sites

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);

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.

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);
}

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);

 

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.