|
-
Apr 2nd, 2008, 06:41 PM
#1
Thread Starter
Hyperactive Member
how to use the replace function to replace consecutive DIVs with IDs
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",...
PHP Code:
$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:
Code:
<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>
-
Apr 2nd, 2008, 06:59 PM
#2
Re: how to use the replace function to replace consecutive DIVs with IDs
try using
$i++; instead of $i = $i + 1
My usual boring signature: Something
-
Apr 2nd, 2008, 08:17 PM
#3
Re: how to use the replace function to replace consecutive DIVs with IDs
 Originally Posted by dclamp
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.
-
Apr 2nd, 2008, 08:20 PM
#4
Re: how to use the replace function to replace consecutive DIVs with IDs
 Originally Posted by gilgalbiblewhee
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",...
PHP Code:
$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:
Code:
<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 to make these changes though rather than the more unreliable sting replace.
-
Apr 2nd, 2008, 11:22 PM
#5
Re: how to use the replace function to replace consecutive DIVs with IDs
 Originally Posted by visualAd
Excellent idea. Replace a line of code with an equivalent line of code. I am sure that is going to solve the problem. 
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.
My usual boring signature: Something
-
Apr 3rd, 2008, 12:12 AM
#6
Re: how to use the replace function to replace consecutive DIVs with IDs
 Originally Posted by dclamp
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"
-
Apr 3rd, 2008, 12:21 AM
#7
Re: how to use the replace function to replace consecutive DIVs with IDs
oh. ok. didnt know if my little 2 cents helped. Guess not.
thanks for the correction!
My usual boring signature: Something
-
Apr 3rd, 2008, 06:10 AM
#8
Re: how to use the replace function to replace consecutive DIVs with IDs
 Originally Posted by dclamp
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:
Code:
$i=1;
echo($i++ * 5); // outputs 5
echo(++$i * 5);; // outputs 10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|