Forum Moderators: coopster

Message Too Old, No Replies

String minus plus etc

String minus plus

         

bono

9:57 am on Jun 27, 2005 (gmt 0)

10+ Year Member



Hi guys, Im lookin at some interesting string manipulation.. and im new to this so havnt really a clue.. does this look right?

function edit_firmness($itemid,$firmness)
{ //changes an items firmness rating
$itemidlength = strlen($itemid);
$curfirmlength = strlen($itemfirmness[$itemid]);
$finstringlength = $itemidlength - $curfirmlength -1;
$newitemid =substr($itemid, 0, $finstringlength);
$this->itemid = $newitemid;

}

mcibor

2:20 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First of all you don't have
$itemfirmness
defined, therefore
$curfirmlength = strlen($itemfirmness[$itemid]);
equals 0
Therefore you get $itemid without last letter.

To do that I recommend using to full extent function substr:

function edit_firmness($itemid,$firmness)
{ //changes an items firmness rating
$cut = strlen($itemfirmness[$itemid]) * (-1);
$newitemid =substr($itemid, 0, $cut);
$this->itemid = $newitemid;

}

It's not very correct, but a bit shorter.

I still have no idea what your function does
Michal Cibor

bono

8:53 am on Jun 28, 2005 (gmt 0)

10+ Year Member



all it does is change the firmness of a porduct in somebodys basket...eg

KN-STD-KI-1 - Standard King Size 1 £459.00

so my function changes the 1's to say 3's

KN-STD-KI-3 - Standard King Size 3 £459.00

bono

11:27 am on Jun 28, 2005 (gmt 0)

10+ Year Member



still cant get it to work.. anyone else got a clue?

mcibor

8:10 am on Jun 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you've got a string: 3 words + 1 word to change + some words?

function change_word($str, $to, $which) {
$array = explode(" ", $str);
$array[$which] = $to;
return implode(" ", $array);
}

To change fourth word:
$str = "Standard King Size 1 £459.00";
$str3 = change_word($str, "3", 3);
//$str3 = "Standard King Size 3 £459.00";

Best regards
Michal Cibor