I am having a hard time with converting these
[URL="http://___."]Text Here[/URL]
[EMAIL="@_.__"]Text Here[/EMAIL]
to
<a href = "http://__." target = "blank">Text Here</a>
<a href = "mailTo:@.">Text Here</a>
I am having a hard time with converting these
[URL="http://___."]Text Here[/URL]
[EMAIL="@_.__"]Text Here[/EMAIL]
to
<a href = "http://__." target = "blank">Text Here</a>
<a href = "mailTo:@.">Text Here</a>
woops.. I guess that will actually work on this board. Let me try that again. I put spaces in there so it doesnt get parsed.
[ URL="http://_." ]Text Here [ / URL ]
[ EMAIL="@." ]Text Here [ / EMAIL ]
The following seem to work after a quick test
// Substitute the URL
$subject = '[URL="http://____.__" ]Text Here[/URL]';
$pattern = '/\\[URL=\"(.+?)\"\\](.+?)\\[\\/URL\\]/';
$replacement = '<a href="\\1" target="_blank">\\2</a>';
$output = preg_replace($pattern, $replacement, $subject);
// Output the result
echo '<p>'. htmlspecialchars($output) . '</p>';
// Substitute the EMAIL
$subject = '[EMAIL="___@____.__" ]Text Here[/EMAIL]';
$pattern = '/\\[EMAIL=\"(.+?)\"\\](.+?)\\[\\/EMAIL\\]/';
$replacement = '<a href="mailTo:\\1">\\2</a>';
$output = preg_replace($pattern, $replacement, $subject);
// Output the result
echo '<p>'. htmlspecialchars($output) . '</p>';
The URL and EMAIL tags have a space before the first closing bracket ] for this board to show the code properly.
EDIT: doubled the backslash characters for the forum to show them correctly.
thanks
When I try this code I am getting this error
Warning: Unknown modifier 'R' in /home/trackiec/public_html/mrkProject/newSite/poweruser/includes/artistNewsDelete.php on line 38
this is line 38
$news = preg_replace($pattern, $replacement, $news);
Any ideas??
Another version of Uffi's post:
Of course, you will have to remove all space in the [tags] from the variable $txt
$txt='her your text to be parsed';
$in=array(
'#\[URL([^]]*)\]([^[]*)\[/URL\]#',
'#\[EMAIL="([^"]*")\s*\]([^[]*)\[/EMAIL\]#');
$out=array(
'<a href$1 target = "_blank">$2</a>',
'<a href="mailTo:$1">$2</a>');
echo preg_replace($in, $out, $txt);
Adam:
The forum parsed my post and some backslash characters went missing as a result of it.
I doubled certain backslashes in my example and now the forum displays them correctly and they should work for you, too.
The reason was that the closing URL tag was interpreted as the end delimiter of the regular expression, which doesn't recognize the modifier R. The first letter U is recognized as the ungreedy modifier.