View Full Version : More Javascript help - Sorry Guys


liam1412
13-07-2007, 00:13
More Javascript help

I was given this script buy a guy on another forum but I can't get hold of him now.

Basically it wraps bbcode around highligted text.

It works fine for the bold and font tags.

I also use it for urls

The problem with this is because of how the script is written and my Parser all I can get it to display for the url text is click here

Here is the javascript

[CODE]

Hi

This script was given to me by someone on here for use on my site. I know nothing of JS at all but he offered this for use (but can't remember who it was)


[code]<script type="text/javascript">
function AddEmo(tag)
{
var emo=document.forms["form"].elements["text"];
if (document.selection)
{
emo.focus();
var sel = document.selection.createRange();
sel.text = tag
}
else if (emo.selectionStart || emo.selectionStart == '0')
{

var startPos = emo.selectionStart
var endPos = emo.selectionEnd
emo.value = emo.value.substring(0, startPos)
+ tag
+ emo.value.substring(endPos, emo.value.length)
}
else {
emo.value += tag;
}
}
function AddTag(tag)
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();

}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
var stri1 = txt
var stri = "["+tag+"]"+stri1+"[/"+tag+"]"

AddEmo(stri);
}

// calling the function
</script>
[/code/]

and here is my parser



function BBCODE($bbcode)
{
//the bbcode tags..
$bbc_a=array(
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"::smile::",
"::mad::",
"::sad::",
"::rolleyes::",
"::smirk::",
"::wink::",
"::surprised::",
"::thinking::",
"::tongue::",
"::cool::",
"",
"",
"",
"",
"",
"",
"",
"",
);

//bbcode gets converted to..
$bbc_b=array(
"<strike>",
"</strike>",
"<b>",
"</b>",
"<u>",
"</u>",
"<font size=4>",
"</font>",
"<font size=8>",
"</font>",
"<a href=\"mailto:",
"\">Click Here</a>",
"<a href=\"",
"\" target=\"blank\">Click Here</a>",
"<a href=\"",
"\" target=\"blank\">Click Here</a>",
"<img src='images/smilies/smile.gif' border='0' />",
"<img src='images/smilies/mad.gif' border='0' />",
"<img src='images/smilies/sad.gif' border='0' />",
"<img src='images/smilies/rolleyes.gif' border='0' />",
"<img src='images/smilies/smirk.gif' border='0' />",
"<img src='images/smilies/wink.gif' border='0' />",
"<img src='images/smilies/surprised.gif' border='0' />",
"<img src='images/smilies/thinking.gif' border='0' />",
"<img src='images/smilies/tongue.gif' border='0' />",
"<img src='images/smilies/cool.gif' border='0' />",
"<img src='",
"' border='2' />",
"<img src='",
"' border='2' />",
"<div class='quote'>Quote:<br />",
"</div>",
"<div class='quote'>Quote:<br />",
"</div>",
);

$bbc_num=count($bbc_a);
$loop=0;
while($loop<$bbc_num)
{
$bbcode=str_replace($bbc_a[$loop], $bbc_b[$loop], $bbcode);
$loop++;
}

return $bbcode;
}

?>

what would I need to do this to either a - display the url itself as the display text.

Thanks as always

John
13-07-2007, 23:54
I'd personally rewrite this myself because you are going to end up with broken HTML such as

[URK]www.sheffieldForum.co.uk[/URL]

would become [URK]www.sheffieldForum.co.uk</a>

Now, Unless the [URL] pairs are guarrenteed, there is no way you could extract www.sheffieldForum.co.uk to produce the output like you want.

<strike> and <s> are obsolete use <del> instead.

Ghozer
14-07-2007, 02:36
here's the BBcode parser I made ;)


function bb2html($text)
{
$bbcode = array(
"", " ", "",
"", "",
"", "",
"", "",
"", "",
"", "",
"[<]", "[>]",
"[br]",
'', '',
'[color="', "[/color]",
"[size=\"", "[/size]",
'[url="', "[/url]",
"[mail=\"", "[/mail]",
"", "",
"", "",
'"]');

$htmlcode = array(
"<ul>", "<li>", "</ul>",
"<img style=\"border:0;width:50%;height:50%\" src=\"", "\" alt=\"Image\"/>",
"<img style=\"border:0\" src=\"", "\" alt=\"Image\"/>",
"<b>", "</b>",
"<u>", "</u>",
"<i>", "</i>",
"&lt;", "&gt;",
"<br />",
'<a id="', '"></a>',
"<span style=\"color:", "</span>",
"<span style=\"font-size:", "</span>",
'<a href="', "</a>",
"<a href=\"mailto:", "</a>",
"<table width=550px bgcolor=darkgray><tr><td bgcolor=darkgray><i>Code:</i></tr></td><tr><td bgcolor=white><code>", "</code></td></tr></table>",
"<table width=550px bgcolor=darkgray><tr><td bgcolor=darkgray><i>Quote:</i></tr></td><tr><td bgcolor=white>", "</td></tr></table>",
'">');
$newtext = str_replace($bbcode, $htmlcode, $newtext);
return $newtext;
}


URLs work as [ URL = "address.com" ] Text Here [ /url ] (without the spaces)


obviously just $bb2html() anything you want to parse

Ghozer
14-07-2007, 02:37
Except these forums have parseed my BB Code for bold/underline/italic -- :(

liam1412
14-07-2007, 12:32
Cheers Ghozer thats cracking as always. You have a good birthday.

liam1412
14-07-2007, 12:53
Ghozer

Its saying called to undefined variable $newtext at this line

$newtext = str_replace($bbcode, $htmlcode, $newtext);

any ideas mate.

Ghozer
14-07-2007, 20:08
sorry, change the $newtest to $text

$newtext = str_replace($bbcode, $htmlcode, $text);

liam1412
14-07-2007, 20:38
Thats working a treat that. Again I will atribute the source. Even though it may not pass loads of click throughs to your site it always helps to have links out there.

Legendary mate

Ghozer
15-07-2007, 10:58
Hehe, no problem dude...

im sure you can figure out from that how to add more to it any way ;)

and yeah, im not bothered so much about hits, as the more links to my site the better, cause thats how google ranks pages, which in turn will indirectly get us hits any way ;)