Page 1 of 1

Arrays within arrays?

Posted: Fri Jan 10, 2003 9:59 am
by Elmseeker
:? I am adding BB Code to my news posting and want to know if it's possible to have an array within an array, here is the code then I will explain further what I would like to do with it:

Code: Select all

<?php
// BBCode Parsing Function...
include("bb_array.php"); // Where the actual BBCode definition table is stored...
function convert_bbcodes( $text )
{ 
  foreach( $GLOBALS['bb_codes'] as $search=>$replace )
  {  
     $text = str_replace( $search,$replace,$text );
  }  
  return $text;
}
?>
And the array itself:

Code: Select all

<?php
$bb_codes = array(
    # bold
    '[b]' => '<span style="font-weight:bold">',
    '[/b]' => '</span>',
    # italic
    '[i]' => '<span style="font-style:italic">',
    '[/i]' => '</span>',
    # underline
    '[u]' => '<span style="text-decoration:underline">',
    '[/u]' => '</span>',
    # colors
    '[red]' => '<span style="color:red">',
    '[aqua]' => '<span style="color:aqua">',
    '[black]' => '<span style="color:black">',
    '[blue]' => '<span style="color:blue">',
    '[fuchsia]' => '<span style="color:fuchsia">',
    '[gray]' => '<span style="color:gray">',
    '[green]' => '<span style="color:green">',
    '[lime]' => '<span style="color:lime">',
    '[maroon]' => '<span style="color:maroon">',
    '[navy]' => '<span style="color:navy">',
    '[olive]' => '<span style="color:olive">',
    '[purple]' => '<span style="color:purple">',
    '[silver]' => '<span style="color:silver">',
    '[teal]' => '<span style="color:teal">',
    '[white]' => '<span style="color:white">',
    '[yellow]' => '<span style="color:yellow">',
    '[/color]' => '</span>',
    '[/colour]' => '</span>', # For our british friends.
    # smilies
    ':)' => '<img src="/images/smiles/icon_smile.gif" />',
    ':(' => '<img src="/images/smiles/icon_sad.gif" />',
    ';)' => '<img src="/images/smiles/icon_wink.gif" />',
    ';>' => '<img src="/images/smiles/icon_twisted.gif" />',
    ':D' => '<img src="/images/smiles/icon_biggrin.gif" />',
    ':?' => '<img src="/images/smiles/icon_confused.gif" />',
    '8)' => '<img src="/images/smiles/icon_cool.gif" />',
    ';(' => '<img src="/images/smiles/icon_cry.gif" />',
    '#)' => '<img src="/images/smiles/icon_rolleyes.gif" />',
    '[?]' => '<img src="/images/smiles/icon_question.gif" />'
);
?>
:?: Ok, what I would like to do is within this array have another array that contains various curses, swear words and other things we don't want allowed on the site due to the fact that it is a site for kids and have all of them replaced by the same thing, in this case we want to have swears replaced with: ":o I'm a potty mouth :o", I don't think people will want to see this on their posts and will therefor not swear very often lol...besides kids will like it. :) Would it possible to do this? Thanks!

[Edit]
Oops, it seems that phpBB has stripped out and parsed all of my bbcode lol...anyway I am sure you can get the jist from this, if not I'll have to find another way to post the array...

Posted: Fri Jan 10, 2003 10:12 am
by jason
Arrays inside arrays are called multidimensional arrays.

Code: Select all

<?php
$array[] = array('Name'=>'Jason Lotito', 'Age'=>22);
$array[] = array('Name'=>'Piera Paci', 'Age'=>24);
$array[] = array('Name'=>'Bob Jackson', 'Age'=>26);

print_r($array);
?>
Now, $array[0] is an array with Jason's information, $array[1] has Piera's, and $array[2] has Bob's. You can of course go further:

Code: Select all

<?php
$array[] = array('Name' => array('First'=>'Jason', 'Last'=>'Lotito'), 'Age'=>22);
?>
Without the argument whether that is a good way to do the above, it does give an example of how to nest arrays.

Posted: Fri Jan 10, 2003 10:26 am
by Elmseeker
jason wrote: Without the argument whether that is a good way to do the above, it does give an example of how to nest arrays.
If there is a better way to do this please do tell, I would like my code to be as clean and efficient as possible. I am also at a loss as to how to use the above function to replace strings within the nested array if I were to go that route...

Let me also see if I have this right so far:

:?:

Code: Select all

<?php
$bb_codes = array(
    '#)' => '<img src="/images/smiles/icon_rolleyes.gif" />',
    '[?]' => '<img src="/images/smiles/icon_question.gif" />'
    $bb_code[] = array("f-word"=>"I'm a potty mouth", "s-word"=>"I'm a potty mouth", etc...);
);
?>
Thanks...

Posted: Fri Jan 10, 2003 10:42 am
by jason
Actually, it would be

Code: Select all

<?php

$bb_codes = array(
    '#)' => '<img src="/images/smiles/icon_rolleyes.gif" />',
    '[?]' => '<img src="/images/smiles/icon_question.gif" />',
    'curseWords' => array("f-word"=>"I'm a potty mouth", "s-word"=>"I'm a potty mouth", etc...)
);
?>
As far as there "being a better way", I was refering to the fact that I had added another dimension to abstract Name to First and Last. Rather than add another array, I would just have used 'firstName', 'lastName', and 'middleName'.

The function you are talking about doesn't use a multidimensional array. It uses a single dimension array, which may be the way you want to go.

Posted: Fri Jan 10, 2003 11:56 am
by Elmseeker
jason wrote: The function you are talking about doesn't use a multidimensional array. It uses a single dimension array, which may be the way you want to go.
Ok, but in a single dimensional array can I associate more than 1 item with the same "variable" (for lack of a better word)? Meaning could I do something like:

'f-word', 'b-words', 's-word' => "I am a potty mouth"


or would it have to be like:

'f-word' => "I am a potty mouth",
'b-word' => "I am a potty mouth",
's-word' => "I am a potty mouth"
?

Thank you for all the help so far, I really appreciate it!

Posted: Fri Jan 10, 2003 2:23 pm
by skehoe
Hey,

Personally, I would do something easier to edit like this:

Code: Select all

// list of bad words
$bad = array('s-word', 'b-word', 'f-word');

// Generate censored array
$censored = array();
foreach ($bad as $key => $val) &#123;
    $censored&#1111;$val] = 'I am a potty mouth';
&#125;
Hope that helps.

~Scott

Posted: Fri Jan 10, 2003 2:53 pm
by Elmseeker
Well...I decided to make a second array of the bad words and use str_replace as it wa sbefore, here is what I have now...

Code: Select all

<?php
function convert_bbcodes( $text ) 
{ 
  foreach( $GLOBALS['bb_codes'] as $search=>$replace )
  {
     $text = str_replace( $search,$replace,$text );
  }
  foreach( $GLOBALS['badwords'] as $search )
  {
     $text = str_replace( $search,"<IMG SRC="images/smiles/icon_eek.gif"> I am a Potty mouth!
     <IMG SRC="images/smiles/icon_eek.gif">",$text );
  }

  return $text;
}
?>
Unfortunately my array has a few elements that have spaces (for multiple words, like pu*** licker) and others that have no spaces but additions like fu** and fu**ing, I put fu**ing before fu** in the array (also tried after to be sure) but the output I get from these is as such...

Code: Select all

This is a &lt;IMG SRC="images/smiles/icon_eek.gif"&gt; I am a Potty mouth!
     &lt;IMG SRC="images/smiles/icon_eek.gif"&gt;ing test of the &lt;IMG SRC="images/smiles/icon_eek.gif"&gt; I am a Potty mouth!
     &lt;IMG SRC="images/smiles/icon_eek.gif"&gt; licking swear filter!
Any idea how to fix this problem? Thanks!
BTW the output above is the HTML generated by the PHP and displayed in browser...

Posted: Fri Jan 10, 2003 4:56 pm
by volka
mixed str_replace ( mixed search, mixed replace, mixed subject) can take an array as search-pattern, e.g.

Code: Select all

<?php
$erase = array('jerks', 'jerk', 'nitwit');
$text = '...but turned out to be complete jerks with no sense of humor. signed: The nitwit';

echo str_replace($erase, '*<IMG SRC="images/smiles/icon_eek.gif"/>*', $text)
?>
;)

Posted: Fri Jan 10, 2003 6:18 pm
by Elmseeker
So are you saying I don't need to use the foreach statement?

Posted: Fri Jan 10, 2003 7:10 pm
by volka
the answer is within the example ;)