/    Sign up×
Community /Pin to ProfileBookmark

Help My BBCode Function

[code=php]<?php // if someone tries to access this file directly from a browser, block them

if (realpath(__FILE__) == realpath($_SERVER[‘DOCUMENT_ROOT’] . ‘/’ . $_SERVER[‘SCRIPT_NAME’]))
{

die(‘Access Denied’);

}

function BBCODE($input)
{

$pattern = array();

$pattern[0] = “/[[Bb]](.*)[/[Bb]]/”;
$pattern[1] = “/[[Ii]](.*)[/[Ii]]/”;
$pattern[2] = “/[url=(.*)](.*)[/url]/i”;

$replace = array();

$replace[0] = “<b>$1</b>”;
$replace[1] = “<i>$1</i>”;
$replace[2] = “<a href=”$1″ target=”_blank”>$1</a>”;

$input = preg_replace($pattern, $replace, $input);

return $input;

}

?>[/code]

Okay, so I’m using REGEX to format my BBCode. The bold and italic both come out fine, but when I try to use url, something weird happens. It formats it into a url, but for some reason, it does so by adding the url as an extension to your current url. Instead of posting the link to go to [url]www.something.com[/url], it posts it to go to [url]www.something.com/directory/www.something.com[/url].

What should I do to fix this?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ShortsNov 04.2008 — Make sure there is an http:// at the beginning.

So I'd say add the http:// and then replace any http://http:// with http://

E.g.:

[code=php]
$replace[2] = "<a href="http://$1" target="_blank">$1</a>";
[/code]


[code=php]
$input = str_replace('http://http://','http://',$input);
[/code]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorNov 04.2008 — Thanks? That fixed that part, but ever since I made that edit, the output is now displaying the full url, instead of the link name that leads to that url?
Copy linkTweet thisAlerts:
@David_HarrisonNov 04.2008 — Would it not be:$replace[2] = "&lt;a href="http://$1" target="_blank"&gt;$[color=red]2[/color]&lt;/a&gt;";
Copy linkTweet thisAlerts:
@ayveghNov 04.2008 — Totally unrelated, but I just used your original search-and-replace function for something else! ?

Thanks,

ayvegh

EDIT:

Sorry for stealing your thread, Joseph, but how can I turn this:
[code=php] $pattern[0] = "/[[Bb]](.*)[/[Bb]]/"; [/code]
into something like this?
[code=php] $pattern[0] = "name_of_some_php_func(' (.*) ')"; [/code]
Thanks to anyone who anwsers, and sorry for stealing your thread, Joseph ?
Copy linkTweet thisAlerts:
@David_HarrisonNov 04.2008 — That was basically it:$pattern[0] = "/name_of_some_php_func('(.*?)')";I took the spaces out since otherwise it will try and match them. I also made it a non-greedy match of content inside the quotes, otherwise on this code:name_of_some_php_func('blah');name_of_some_other_php_func('blah again');It would match:blah');name_of_some_other_php_func('blah again

Joseph, you may want to do the same for your patterns, a greedy operator will always match the absolute most it can.
Copy linkTweet thisAlerts:
@NogDogNov 04.2008 — You may find that as you start getting into multiple, nested tags, things start to get rather nasty. You may find it is then both simpler and more efficient to use either the [url=http://php.net/bbcode]bbcode extension[/url] or the [url=http://pear.php.net/package/HTML_BBCodeParser]PEAR HTML_BBCodeParser[/url] package.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorNov 12.2008 — All right, thanks!
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...