PDA

Click to See Complete Forum and Search --> : how to use the replace function to replace consecutive DIVs with IDs


gilgalbiblewhee
Apr 2nd, 2008, 06:41 PM
I'm trying to understand where I went wrong I need to insert by replacing <DIV with <DIV ID="div0", <DIV ID="div1", <DIV ID="div2", <DIV ID="div3",...
$i=0;
function divCount(){
$i = $i + 1;
$iString = $i + "";
return "div".$iString;
}
$strResult = str_replace("<DIV STYLE=", "<DIV ID='".divCount()."' STYLE=", $result);
echo $strResult;

This is what's happening so far:
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:69;LEFT:177" CLASS="APFont00000">...</DIV>
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:88;LEFT:290" CLASS="APFont00001">...</DIV>
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:103;LEFT:231" CLASS="APFont00001">...</DIV>
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:154;LEFT:71" CLASS="APFont00002">...</DIV>

dclamp
Apr 2nd, 2008, 06:59 PM
try using

$i++; instead of $i = $i + 1

visualAd
Apr 2nd, 2008, 08:17 PM
try using

$i++; instead of $i = $i + 1
Excellent idea. Replace a line of code with an equivalent line of code. I am sure that is going to solve the problem. :rolleyes:

visualAd
Apr 2nd, 2008, 08:20 PM
I'm trying to understand where I went wrong I need to insert by replacing <DIV with <DIV ID="div0", <DIV ID="div1", <DIV ID="div2", <DIV ID="div3",...
$i=0;
function divCount(){
$i = $i + 1;
$iString = $i + "";
return "div".$iString;
}
$strResult = str_replace("<DIV STYLE=", "<DIV ID='".divCount()."' STYLE=", $result);
echo $strResult;

This is what's happening so far:
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:69;LEFT:177" CLASS="APFont00000">...</DIV>
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:88;LEFT:290" CLASS="APFont00001">...</DIV>
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:103;LEFT:231" CLASS="APFont00001">...</DIV>
<DIV ID='div1' STYLE="POSITION:ABSOLUTE;TOP:154;LEFT:71" CLASS="APFont00002">...</DIV>
Your $i variable is not declared as global in the function. I would advise the you use DOM (http://www.php.net/DOM)to make these changes though rather than the more unreliable sting replace.

dclamp
Apr 2nd, 2008, 11:22 PM
Excellent idea. Replace a line of code with an equivalent line of code. I am sure that is going to solve the problem. :rolleyes:
Hey, it is going to help his code look nicer. Maybe PHP didnt like how it looked.

I re-looked your code and i am not seeing anything wrong with it. Is your function being included properly?

edit:
I am not sure, but i think that the $i=0 might me reseting it and it keeps adding 1 to 0.

lintz
Apr 3rd, 2008, 12:12 AM
I re-looked your code and i am not seeing anything wrong with it. Is your function being included properly?


VisualAd said it all - "Your $i variable is not declared as global in the function"

dclamp
Apr 3rd, 2008, 12:21 AM
oh. :p ok. didnt know if my little 2 cents helped. Guess not.

thanks for the correction!

visualAd
Apr 3rd, 2008, 06:10 AM
Hey, it is going to help his code look nicer. Maybe PHP didnt like how it looked.

I re-looked your code and i am not seeing anything wrong with it. Is your function being included properly?

edit:
I am not sure, but i think that the $i=0 might me reseting it and it keeps adding 1 to 0.
Don't take this the wrong way; but your suggestion wasn't helpful because it had nothing to do with the problem. I would personally go for ($i+=1) for readability purposes but if you want to use the increment operator on its own use (++$i) which is more efficient.

The difference between ++$i and $i++ is when the variable is increment. The former is incremented before the expression is executed and the latter is executed after the expression is executed.

e.g:

$i=1;

echo($i++ * 5); // outputs 5
echo(++$i * 5);; // outputs 10