Jump to content

How to deal with the $array you got from exploding a variable


horsebox

Recommended Posts

I have a simple BBCode system but there are a few special tags that need to run PHP functions so what I do is I explode the text field like this

	
$br_pieces = explode('~T~',$replace); 

for ($ibr=0; $ibr<=10; $ibr++) { 

	if ($ibr % 2) { $br_number = "odd"; } else { $br_number = "even"; }

	if ($br_number == "even") { echo $br_pieces[$ibr]; }
	else { php_function($br_pieces[$ibr]); }
   }
}

It works but this method is causing me problems. When I explode the variable there may be anywhere from 2 to 20 parts and as you can see even numbers is text to be echoed and odd numbers are functions to be executed. I have to systematically process every piece of the exploded variable. What kinda loop would you use for this? I only really know how to use for loops but they don't seem to be the right type of loop for this kinda thing. Either that or I just cant use them properly.

leehanken, just take a string eg: string1~T~string2~T~string3

As I believe thats the format in the database.

What horsebox is saying.. the amount of strings in the database in that one string.. if that makes sense.. can be anywhere between 2 and 20.

The explode, will get all the parts, though he's looking for a way to check them all.. using a loop... bare in mind.. the number of outputs is random. His method.. currently will only check up to 10 parts..

 

Horsebox, I had this issue once.. but I was using two arrays... I'm not really sure on how I can do it with yours, I know what the problem your having is though :)

try...

 

do the explode

 

count the number of elements

 

divide by 2 (this is because in realty the elements are used in pairs)

 

loop thru the array

 

$br_pieces = explode('~T~',$replace);
$el_count = (count($br_pieces)/2);
$ibr=0;
while($ibr<$el_count) {
  echo $br_pieces[$ibr];
  php_function($br_pieces[$ibr+1]);
  $ibr = $ibr + 2;
]

TeddyKiller: Yeah thats what I'm talking about for example the string will be "TEXT~T~FUNCTION~T~TEXT~T~FUNCTION~T~TEXT" so when I explode it all the odd numbered array elements are text to be echoed and the even numbers are functions to be executed.

litebearer: I'll try that now thanks. BTW I like your signature

"The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture"

I think mainly in pictures so I'm a big picture thinker myself too. Probably why I like programming so much because with programming when you learn the basics you can start putting things together and building functions. You can start putting your functions together and building applications. You can start putting your applications together and building huge multipurpose programs.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.