#1 (permalink)  
Old 04-02-2008, 07:45 PM
Registered User
 
Join Date: Jul 2004
Posts: 249
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>
Reply With Quote

  #2 (permalink)  
Old 04-03-2008, 01:08 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
There are two main problems with your code that are both causing the unexpected result you're achieving.

First, you are not understanding the scope of PHP variables properly.
Secondly, you are not looping through the divs withing your string, you're just telling PHP to go through the string one time and replace all of the instances of "div" with an identified div. Therefore, the function is only called once, because the str_replace function is only called once.

A standard variable within a function will stay within that function, and will never have any bearing on variables outside of that function.

That said, I will get a little more specific by adding some comments to your code:
PHP Code:
$i=0# Here, you set the global variable $i to 0
function divCount(){ # Here, you begin your function definition
$i $i 1# Here, you increase the value of a brand-new variable called $i within your function.  PHP interprets the value of the undeclared variable as 0.  You could really do this step as $i++; instead of $i = $i + 1; to make things a little simpler
$iString $i ""# This step is unnecessary, as, PHP will interpret the integer as a string when you append it to your return statement anyway
return "div".$iString# Because I am recommending that you remove the statement above, this should become "div".$i
# Here you close your function definition
$strResult str_replace("<DIV STYLE=""<DIV ID='".divCount()."' STYLE="$result); # Here you are attempting to replace all instances of "<DIV..." with "<DIV ID...", with the ID being incremented by your function.  However, as described above, you're really only running the "divCount" function once.
echo $strResult
In order for the function to operate the way you intend, you will need to add $i as a parameter in your function, and include it byRef instead of the standard byVal. To include a variable byRef, you need to precede the variable name with an ampersand (&) when declaring it inside your function opening. For example:
PHP Code:
function divCount($i) { # This opening will simply accept the value of $i, but any changes made to that variable within the function will be thrown away when the function is finished

function divCount(&$i) { # This opening will accept the actual variable $i, and any changes made to that variable within the function will stay that way even after the function is finished. 
Now, onto problem number two, which is slightly more complicated. You really need to loop through each occurrence of divs within your string. Otherwise, as mentioned above, you're only invoking the divCount function once.

I can't really come up with a reliable way to do that at the moment, but if you don't come up with anything, let me know and I'll try to think about it. Good luck.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie help with simple function elaine Programming and Scripting 2 07-03-2007 03:04 PM
replace function doesn't work gilgalbiblewhee Programming and Scripting 0 06-19-2007 05:01 PM
Jump to a sub Function from a link???? NevadaSam Programming and Scripting 2 05-08-2006 11:37 AM
showLayer function, layer position problem ruthann Programming and Scripting 3 03-19-2006 11:13 AM
How do I highlight a split function text? gilgalbiblewhee Programming and Scripting 7 01-31-2006 05:12 PM


All times are GMT -5. The time now is 11:22 AM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter