Results 1 to 8 of 8

Thread: how to use the replace function to replace consecutive DIVs with IDs

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    447

    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>
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to use the replace function to replace consecutive DIVs with IDs

    Quote 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to use the replace function to replace consecutive DIVs with IDs

    Quote 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: how to use the replace function to replace consecutive DIVs with IDs

    Quote 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

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: how to use the replace function to replace consecutive DIVs with IDs

    Quote 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"

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: how to use the replace function to replace consecutive DIVs with IDs

    Quote 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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width