Jump to content

foreach and array need help combining


doa24uk

Recommended Posts

Hi guys.

 

Here's the code

 

		array(
			'tag' => 'code',
			'type' => 'unparsed_content',
			'content' => '<div class="codeheader">' . $txt['smf238'] . ':</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">
$1</pre>' : '$1') . '</div>',
		),

 

And what I want to do is explode $1 & add something to the end of it (a new variable.

 

eg.

 

$1 - $2

 

So here's my explode code, I'm getting errors wherever I place it.

 

$2 = '';
$codeChunks = explode("\n", $1);

foreach($codeChunks as $i => $chunk)
{
$2 .= "$chunk - <b><a href='http://mysite.com?id=$chunk' target='_blank'>Hyperlink</a></b>\n";
}

 

So hopefully I end up with an output like this ....

 

1 - [url=http://mysite.com?id=1]Hyperlink[/url]
2 - [url=http://mysite.com?id=2]Hyperlink[/url]
3 - [url=http://mysite.com?id=3]Hyperlink[/url]
4 - [url=http://mysite.com?id=4]Hyperlink[/url]
5 - [url=http://mysite.com?id=5]Hyperlink[/url]

Link to comment
Share on other sites

Variables cannot start with a number.

 

The original (array) code is from SMF (Simple Machines Forum) & they use the $1 variable just fine...

 

I should probably point out that this array is actually inside another array...

 

Here's the entire code to hopefully make it clearer.

 

	/* The following bbc are formatted as an array, with keys as follows:

		tag: the tag's name - should be lowercase!

		type: one of...
			- (missing): [tag]parsed content[/tag]
			- unparsed_equals: [tag=xyz]parsed content[/tag]
			- parsed_equals: [tag=parsed data]parsed content[/tag]
			- unparsed_content: [tag]unparsed content[/tag]
			- closed: [tag], [tag/], [tag /]
			- unparsed_commas: [tag=1,2,3]parsed content[/tag]
			- unparsed_commas_content: [tag=1,2,3]unparsed content[/tag]
			- unparsed_equals_content: [tag=...]unparsed content[/tag]

		parameters: an optional array of parameters, for the form
		  [tag abc=123]content[/tag].  The array is an associative array
		  where the keys are the parameter names, and the values are an
		  array which may contain the following:
			- match: a regular expression to validate and match the value.
			- quoted: true if the value should be quoted.
			- validate: callback to evaluate on the data, which is $data.
			- value: a string in which to replace $1 with the data.
			  either it or validate may be used, not both.
			- optional: true if the parameter is optional.

		test: a regular expression to test immediately after the tag's
		  '=', ' ' or ']'.  Typically, should have a \] at the end.
		  Optional.

		content: only available for unparsed_content, closed,
		  unparsed_commas_content, and unparsed_equals_content.
		  $1 is replaced with the content of  the tag.  Parameters
		  are repalced in the form {param}.  For unparsed_commas_content,
		  $2, $3, ..., $n are replaced.

		before: only when content is not used, to go before any
		  content.  For unparsed_equals, $1 is replaced with the value.
		  For unparsed_commas, $1, $2, ..., $n are replaced.

		after: similar to before in every way, except that it is used
		  when the tag is closed.

		disabled_content: used in place of content when the tag is
		  disabled.  For closed, default is '', otherwise it is '$1' if
		  block_level is false, '<div>$1</div>' elsewise.

		disabled_before: used in place of before when disabled.  Defaults
		  to '<div>' if block_level, '' if not.

		disabled_after: used in place of after when disabled.  Defaults
		  to '</div>' if block_level, '' if not.

		block_level: set to true the tag is a "block level" tag, similar
		  to HTML.  Block level tags cannot be nested inside tags that are
		  not block level, and will not be implicitly closed as easily.
		  One break following a block level tag may also be removed.

		trim: if set, and 'inside' whitespace after the begin tag will be
		  removed.  If set to 'outside', whitespace after the end tag will
		  meet the same fate.

		validate: except when type is missing or 'closed', a callback to
		  validate the data as $data.  Depending on the tag's type, $data
		  may be a string or an array of strings (corresponding to the
		  replacement.)

		quoted: when type is 'unparsed_equals' or 'parsed_equals' only,
		  may be not set, 'optional', or 'required' corresponding to if
		  the content may be quoted.  This allows the parser to read
		  [tag="abc]def[esdf]"] properly.

		require_parents: an array of tag names, or not set.  If set, the
		  enclosing tag *must* be one of the listed tags, or parsing won't
		  occur.

		require_children: similar to require_parents, if set children
		  won't be parsed if they are not in the list.

		disallow_children: similar to, but very different from,
		  require_children, if it is set the listed tags will not be
		  parsed inside the tag.
	*/

	$codes = array(
		array(
			'tag' => 'code',
			'type' => 'unparsed_content',
			'content' => '<div class="codeheader">' . $txt['smf238'] . ':</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">
$1</pre>' : '$1') . '</div>',
			// !!! Maybe this can be simplified?
			'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
				global $context;

				if (!isset($disabled[\'code\']))
				{
					$php_parts = preg_split(\'~(<\?php|\?>)~\', $data, -1, PREG_SPLIT_DELIM_CAPTURE);

					for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
					{
						// Do PHP code coloring?
						if ($php_parts[$php_i] != \'<?php\')
							continue;

						$php_string = \'\';
						while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != \'?>\')
						{
							$php_string .= $php_parts[$php_i];
							$php_parts[$php_i++] = \'\';
						}
						$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
					}

					// Fix the PHP code stuff...
					$data = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode(\'\', $php_parts));

					// Older browsers are annoying, aren\'t they?
					if ($context[\'browser\'][\'is_ie4\'] || $context[\'browser\'][\'is_ie5\'] || $context[\'browser\'][\'is_ie5.5\'])
						$data = str_replace("\t", "<pre style=\"display: inline;\">\t</pre>", $data);
					elseif (!$context[\'browser\'][\'is_gecko\'])
						$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
				}'),
			'block_level' => true,
		),
		array(
			'tag' => 'code',
			'type' => 'unparsed_equals_content',
			'content' => '<div class="codeheader">' . $txt['smf238'] . ': ($2)</div><div class="code">' . ($context['browser']['is_gecko'] ? '<pre style="margin-top: 0; display: inline;">$1</pre>' : '$1') . '</div>',
			// !!! Maybe this can be simplified?
			'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
				global $context;

				if (!isset($disabled[\'code\']))
				{
					$php_parts = preg_split(\'~(<\?php|\?>)~\', $data[0], -1, PREG_SPLIT_DELIM_CAPTURE);

					for ($php_i = 0, $php_n = count($php_parts); $php_i < $php_n; $php_i++)
					{
						// Do PHP code coloring?
						if ($php_parts[$php_i] != \'<?php\')
							continue;

						$php_string = \'\';
						while ($php_i + 1 < count($php_parts) && $php_parts[$php_i] != \'?>\')
						{
							$php_string .= $php_parts[$php_i];
							$php_parts[$php_i++] = \'\';
						}
						$php_parts[$php_i] = highlight_php_code($php_string . $php_parts[$php_i]);
					}

					// Fix the PHP code stuff...
					$data[0] = str_replace("<pre style=\"display: inline;\">\t</pre>", "\t", implode(\'\', $php_parts));

					// Older browsers are annoying, aren\'t they?
					if ($context[\'browser\'][\'is_ie4\'] || $context[\'browser\'][\'is_ie5\'] || $context[\'browser\'][\'is_ie5.5\'])
						$data = str_replace("\t", "<pre style=\"display: inline;\">\t</pre>", $data);
					elseif (!$context[\'browser\'][\'is_gecko\'])
						$data = str_replace("\t", "<span style=\"white-space: pre;\">\t</span>", $data);
				}'),
			'block_level' => true,
		),

// END SECTION I'M INTERESTED IN

// I'VE LEFT THE FOLLOWING IN TO SHOW HOW OTHER arrays USE $1 as a VARIABLE, SO //THE FOREACH LOOP MUST GO AROUND THE STUFF ABOVE & NOT ALL OF IT.

		array(
			'tag' => 'color',
			'type' => 'unparsed_equals',
			'test' => '(#[\da-fA-F]{3}|#[\da-fA-F]{6}|[A-Za-z]{1,12})\]',
			'before' => '<span style="color: $1;">',
			'after' => '</span>',
		),
		array(
			'tag' => 'email',
			'type' => 'unparsed_content',
			'content' => '<a href="mailto:$1">$1</a>',
			// !!! Should this respect guest_hideContacts?
			'validate' => create_function('&$tag, &$data, $disabled', '$data = strtr($data, array(\'<br />\' => \'\'));'),
		),
		array(
			'tag' => 'email',
			'type' => 'unparsed_equals',
			'before' => '<a href="mailto:$1">',
			'after' => '</a>',
			// !!! Should this respect guest_hideContacts?
			'disallow_children' => array('email', 'ftp', 'url', 'iurl'),
			'disabled_after' => ' ($1)',
		),
		array(
			'tag' => 'size',
			'type' => 'unparsed_equals',
			'test' => '[1-9]\]',
			// !!! line-height
			'before' => '<font size="$1" style="line-height: 1.3em;">',
			'after' => '</font>',
		),
		array(
			'tag' => 'time',
			'type' => 'unparsed_content',
			'content' => '$1',
			'validate' => create_function('&$tag, &$data, $disabled', '
				if (is_numeric($data))
					$data = timeformat($data);
				else
					$tag[\'content\'] = \'[time]$1[/time]\';'),
		),

	);

Link to comment
Share on other sites

I think you're misunderstanding what is going on in that code you have posted. Like Thorpe said, variables can not start with a number.

 

The "variables" that start with a number are not being used as variables as they're within a string enclosed by single quotes. Variables are not parsed within single quoted strings. Numbered "variables" are however used as back references in RegEx, so they could be passing what you're seeing to a RegEx function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.