Jump to content

[SOLVED] Automatically anchoring URLs that are *not* inside [code] tags


Recommended Posts

The background: I have a function that formats text with bold, italics, underlines, images, and automatically anchored URLs. I have a tag (I call it the [raw] tag as in raw-text) for leaving anything inbetween the tags unformatted.

 

The problem: URLs inside the [raw]*[/raw] tags still get automatically anchored.

 

The code:

$strs = array('This is a [i]string[/i] with a link: http://foobar.com/ --  no twix for you.',
	'[raw]This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you[/raw]'
	);
foreach($strs as $str){
	preg_match_all('/\[raw\](.*?)\[\/raw\]/si', $str, $code);
	foreach($code[1] as $stri) $str = '<span style="font-family: monospace; white-space: pre;">'.str_replace('[', '&#91;', $stri).'</span>';
	// THIS IS WHERE THE RAW-TAG REPLACEMENT ENDS
	// ##################################
	// NOW FOR PARSING THE REGULAR TAGS THAT HAVEN'T HAD THEIR LEFT-SQUARE BRACKETS REPLACED:
	$search = array('/^[\&\#91\;raw\]]((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$)^[\&\#91\;\/raw\]]/', '/\[i\](.*?)\[\/i\]/si');
	$replace = array('<a href="$1" rel="nofollow" target="_blank">$1</a>$4', '<i>$1</i>');
	$str = preg_replace($search, $replace, $str);
	echo '<p>'.$str.'</p>';
}

 

What I expect to happen is that the element of the array will be:

This is a <i>string</i> with a link: <a href="http://foobar.com/">http://foobar.com/</a> --  no twix for you.

 

What I expect the second element of the array to look like:

This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you

 

The breakdown of my automatically anchoring Regular-Expression:

'/^[\&\#91\;raw\]]

((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$)

^[\&\#91\;\/raw\]]/'

What this should do is that it looks for URLs not in-between [raw]*[/raw] tags and anchors them. I use ^[\&\#91\;\/raw\]] instead of ^[\[\/raw\]] because the raw-tag works by replacing the first square bracket ([) with it's HTML-entity equivalent (&#91;).

 

A preemptive thanks to any input/advice.

Example:

 

<?php
$strings = array(
'This is a [i]string[/i] with a link: http://foobar.com/ --  no twix for you.',
'[raw]This is a [i]string[/i] with a link: http://foobar.com/ -- no twix for you[/raw]'
);
//define BB code
$replace = array(
'~\[i\](.*?)\[/i\]~is' => '<em>$1</em>',
'~\b(?:mailto:|(?:https?|ftp|nntp|news)://)\S+~i' => '<a href="$0">$0</a>'
);
//define callback function for raw content
function raw_convert($str, $reverse = false) {
$raw = array(
	'[' => '&91;',
	']' => '&93;',
	':' => '&58;'
);
if ($reverse) {
	return str_replace($raw, array_keys($raw), $str);
} else {
	return str_replace(array_keys($raw), $raw, $str);
}
}
foreach ($strings as $string) {
//process raw tags
$out = preg_replace_callback(
	'~\[raw\](.*?)\[/raw\]~is',
	create_function(
		'$matches',
		'return \'[raw]\' . raw_convert($matches[1]) . \'[/raw]\';'
	),
	$string
);
//convert BB code
$out = preg_replace(array_keys($replace), $replace, $out);
//reconvert char substitutes in raw content
$out = preg_replace_callback(
	'~\[raw\](.*?)\[/raw\]~s',
	create_function(
		'$matches',
		'return raw_convert($matches[1], true);'
	),
	$out
);
echo "$out<br /><br />";
}
?>

 

Only drawback I can think of, is that if any of the HTML entities from the raw_convert() function are present inside a pair of [raw] tags, they will be replaced by their character equivalent. But to overcome that you could simply use some other 'unique' tokens instead of their HTML entities.

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.