PDA

View Full Version : Exploding an array



imagined
September 11th, 2008, 01:04 PM
I pull this data from a database:

---------------------------------------
Some explanation goes here

<CODE>
echo 'this is code';
</CODE>

And then I continue the explanation here
---------------------------------------

I want to put this as an array, like this:
[0] = Some explanation goes here
[1] = <CODE>echo 'this is code';</CODE>
[2] = And then I continue the explanation here

I tried



$i=0;
$article = explode('[CODE]', $article);
foreach($article as $key => $value)
{
$array[$i] = $value;
$i++;
}


But it gives me this:

[0] = Some explanation goes here
[1] = <CODE>echo 'this is code';</CODE>And then I continue the explanation here

How can I get the explanation chunks and the CODE chunks in array elements? I guess probably another explode, I have tried but can't make it work.

I'm trying to do this because I want to apply htmlentities to the CODE chunks.

Note: I'm really using brackets instead of < and > but the forum would parse it as code.

imagined
September 11th, 2008, 06:10 PM
This is some code I got online. It's all commented because I'm trying to figure out what everything means, if anybody has any insight please tell.


/**
* A tag function -- change the input to uppercase
* @param string $input The input text to change to uppercase
*/
function tag_b($input) {
$input = callback_prepare('b', $input);
return '<strong>'.$input.'</strong>';
}

/**
* A tag function -- highlight the PHP code
* @param string $input The input text to highlight
*/
function tag_php($input) {
$input = callback_prepare('php', $input);
return highlight_string($input, TRUE);
}

/**
* Apply the formatting for each tag ($tags key) instance defined by the callback function ($tags value) over $text
* @param array $tags The tags and their respective callback functions
* @param string $text The text over which to search for tags
*/
function apply_tags($tags, $text)
{
foreach ($tags as $tag => $func)
{
$text = preg_replace_callback("%\\[$tag\\].*?\\[/$tag\\]%s", $func, $text);
// echo '<p>';
// echo $tag.': '.$func.'<br />';
// echo $text;
// echo '</p>';
}
return $text;
}

/**
* A helper function to the apply_tags callbacks -- sort the array inputs and remove the beginning and lead tags
* @param string $tag The name of the tag over which the calling function is applied
* @param string $text The text that was given to the callback function
*/
function callback_prepare($tag, $text)
{
if (is_array($text)) $text = $text[0];

if (substr($text, 0, strlen($tag)+2) == "[$tag]")
{
$text = substr($text, strlen($tag)+2);
// echo '<p style="background-color:#CCC;">'.$text.'</p>';
}

if (substr($text, -strlen($tag)-3) == "[/$tag]")
{
$text = substr($text, 0, -strlen($tag)-3);
// echo '<p style="background-color:#999;">'.$text.'</p>';
}
// echo '<p style="color:#C00;">'.$text.'</p>';
return $text;
}

// $input = 'this is bold and this is not';
// $input = callback_prepare('b', $input);
// echo $input;

$tags = array(
'b' => 'tag_b',
'php' => 'tag_php',
);
// $source = file_get_contents('source.html');


// $input = '<p>this is bold and this is not</p>';
// echo tag_b($input);


$content = '
Some explanation goes here

this is bold

another line

And then I continue the explanation here

';

echo apply_tags($tags, $content);

eirche
September 11th, 2008, 08:00 PM
explode("\n", $article);

use "\n" as a delimiter