Page 4 of 8
Posted: Fri Apr 13, 2007 10:28 pm
by John Cartwright
If you want to keep it simple then you'll run into walls when it comes to nests or tag mismatches. However, you could do it one line by using str_replace() by passing an array of needles and an array of replacements.
Posted: Sat Apr 14, 2007 10:18 am
by RobertGonzalez
ziggy3000 wrote:do you have a ten line or less code that replaces with <b></b>
the one in cbparser is kinda long, and i like to keep my code short
i added more buttons for url, image, and php code, but it doesn't parse the tags
it parses everything between the <?php and ?> tags in the textfield that you input
the only files i am using are parser-test.php and func.php(my highlight function) so i dont really have a core...
the test page was a little javascript and html and some php
func.php is a function named hc()
Everah wrote:Please do not ask for support as I am still making it work at the moment.
When it is finished I will support questions like this. It is fairly easy to see how things work (a la cbparser) so all of the bbcode tags will work ([ url=
http://www.devnetwork.net ]some text[ /url ] without the spaces) for example.
The code that handles the b tag replacement is a simple str_replace if you look through the code.
Posted: Sat Apr 14, 2007 2:08 pm
by ziggy3000
in the test page this is my code
Code: Select all
<?php
require_once 'func.php';
$string = "<?php
echo 'This Works!';
?>
[b]hello[/b]";
$string = isset($_POST['string']) ? $_POST['string'] : $string;
echo '<h3>Preview</h3>';
function parse_bbcode($bb2html)
{
$bb2html = str_replace('[img]', '<img src="', $bb2html);
$bb2html = str_replace('[/img]', '" >', $bb2html);
$bb2html = str_replace('[b]', '<b>', $bb2html); //ie. "\r\n" becomes "<br />\n"
$bb2html = str_replace('[/b]', '</b>', $bb2html);
$bb2html = str_replace('[i]', '<i>', $bb2html);
$bb2html = str_replace('[/i]', '</i>', $bb2html);
$bb2html = str_replace('[u]', '<u>', $bb2html);
$bb2html = str_replace('[/u]', '</u></span>', $bb2html);
$bb2html = str_replace('[url=', '<a href=', $bb2html);
$bb2html = str_replace('[/url]', '</a>', $bb2html);
$bb2html = str_replace('[quote]', '<cite>', $bb2html);
$bb2html = str_replace('[/quote]', '</cite>', $bb2html);
$bb2html = str_replace('[h2]', '<h2>', $bb2html);
$bb2html = str_replace('[/h2]', '</h2>', $bb2html);
$bb2html = str_replace('[h3]', '<h3>', $bb2html);
$bb2html = str_replace('[/h3]', '</h3>', $bb2html);
$bb2html = str_replace('[h4]', '<h4>', $bb2html);
$bb2html = str_replace('[/h4]', '</h4>', $bb2html);
$bb2html = str_replace('[h5]', '<h5>', $bb2html);
$bb2html = str_replace('[/h5]', '</h5>', $bb2html);
$bb2html = str_replace('[h6]', '<h6>', $bb2html);
$bb2html = str_replace('[/h6]', '</h6>', $bb2html);
$bb2html = str_replace('[', ' <', $bb2html); // you can just replace < and > with [ and ] in your bbcode
$bb2html = str_replace(']', ' >', $bb2html); // for instance, [center] cool [/center] would work!
$bb2html = str_replace('**$@$**', '[', $bb2html);
$bb2html = str_replace('**@^@**', ']', $bb2html);
}
$string2 = parse_bbcode($string);
echo hc($string2,1,1);
?>
but when i do this it doesn't highlight or show anything except for "1" (shows because of line numbers)
the hc function is like this
Code: Select all
hc(code text here,remove tags?,show line numbers?);
help?
Posted: Sat Apr 14, 2007 4:17 pm
by RobertGonzalez
I can't get into this that much this weekend. I am preparing for an out of state trip in a few days.
Out of the box, the code I put in the download handled [ b ] tags without issue. What problem are you having with them?
Posted: Sat Apr 14, 2007 9:43 pm
by ziggy3000
i found a short bbcode script
heres my code now
Code: Select all
<html>
<head>
<title>Parser Test</title>
<script type="text/javascript">
function getSelection(ta) {
var bits = [ta.value,'','',''];
if (document.selection) {
var vs = '#$%^%$#';
var tr = document.selection.createRange();
if (tr.parentElement() != ta) {
return null;
}
bits[2] = tr.text;
tr.text = vs;
fb = ta.value.split(vs);
tr.moveStart('character', -vs.length);
tr.text = bits[2];
bits[1] = fb[0];
bits[3] = fb[1];
} else {
if (ta.selectionStart == ta.selectionEnd) {
return null;
}
bits = (new RegExp('([\x00-\xff]{'+ta.selectionStart+'})([\x00-\xff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\x00-\xff]*)')).exec(ta.value);
}
return bits;
}
function matchPTags(str) {
str = ' ' + str + ' ';
ot = str.split(/\[[B|U|I|URL|IMG].*?\]/i);
ct = str.split(/\[\/[B|U|I|URL|IMG].*?\]/i);
return ot.length == ct.length;
}
function addPTag(ta, pTag) {
bits = getSelection(ta);
if(bits) {
if (!matchPTags(bits[2])) {
alert('\t\tInvalid Selection\nSelection contains unmatched opening or closing tags.');
return;
}
ta.value = bits[1] + '[' + pTag + ']' + bits[2] + '[/' + pTag + ']' + bits[3];
}
}
</script>
</head>
<body>
<?php
$string = "[syntax=php]<?php
echo 'This Works!';
?>
hello";
$string = isset($_POST['string']) ? $_POST['string'] : $string;
echo '<h3>Preview</h3>';
function bbcode($string)
{
$search = array(
'#\[b\](.+?)\[\/b\]#is',
'#\[i\](.+?)\[\/i\]#is',
'#\[u\](.+?)\[\/u\]#is',
'#\[img\](.+?)\[\/img\]#is',
'#\
(.+?)\[\/url\]#is',
'#\[color=(.+?)\](.+?)\[\/color\]#is',
'#\[php\](.+?)\#\[\/php\]#is',
);
$replace = array( //$1 is the content, $2 is also content
'<b>$1</b>',
'<i>$1</i>',
'<span style="border-bottom: 1px solid;">$1</span>',
'<img src="$1">',
'<a href="$1">$2</a>',
'<font color="$1">$2</font>',
'<?php echo hc($1,1,1); ?>',
);
return preg_replace($search, $replace, $string);
}
function hc($code,$ln = 1,$phptags = 1,$name = "")
{
$code_h = highlight_string($code,true);
if (!$phptags)
$code_h = preg_replace("/<\\?php.*?<br \\/>/","",$code_h);
$code_h_lines = explode("<br />",$code_h);
$total_lines = count($code_h_lines);
if ($name !== "")
$name = "<strong>$name</strong><br /><br />";
echo '<br />'.$name.'<div class="phpcode"><table border="0" style="border-collapse: collapse" cellpadding="4" cellspacing="0">
<tr>
';
if ($ln)
{
echo '<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><code style="color: #e28000;">';
for ($a = 0;$a < $total_lines;$a++)
echo ($a+1)."<br />";
echo '</div></td>
</tr>
</table>
';
echo '</td>
';
}
echo '<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
';
for ($a = 0;$a < $total_lines;$a++)
echo ($code_h_lines[$a])."<br />";
echo '
</td>
</tr>
</table>
';
echo '</td>
</tr>
</table></div><br />';
}
$string = bbcode($string);
echo $string;
?>
<fieldset>
<button onclick="addPTag(document.getElementById('text'),'b')"><b>Bold</b></button>
<button onclick="addPTag(document.getElementById('text'),'i')"><i>Italic</i></button>
<button onclick="addPTag(document.getElementById('text'),'u')"><u>Underline</u></button>
<button onclick="addPTag(document.getElementById('text'),'url')"><u><a href="#">Url</a></u></button>
<button onclick="addPTag(document.getElementById('text'),'img')">Image</button>
<button onclick="addPTag(document.getElementById('text'),'php')">Php</button>
<form action="<?php echo basename(__FILE__); ?>" method="post">
<textarea id="text" name="string" cols="150" rows="10"><?php echo $string; ?></textarea></p>
<input type="submit" name="Test" value="Parse" />
</form>
</fieldset>
</body>
</html>
[/syntax]
the problem is that it does only one thing...
if i add
$string = hc($string,1,1);
to the code it highlights it, but it doesnt parse the bbcode
if i dont add it, it parses the bbcode, but it doesn't highlight or show the php
the above script outputs this
[ php] hello
i want it to highlight between Code: Select all
and parse other stuff at the same time
help?
the bbcode tutorial is here
[url]http://www.tutorialninja.com/viewtopic.php?f=7&t=27[/url]
Posted: Mon Apr 16, 2007 1:40 pm
by ziggy3000
i moved this topic to
here because i got a note that i was bumping
any help would be appreciated
Posted: Mon Apr 16, 2007 1:41 pm
by RobertGonzalez
What exactly does the code I posted not do that you want done?
Posted: Mon Apr 16, 2007 1:45 pm
by ziggy3000
i like my highlight function and i feel better if i understand the code that i use... i understand everything that i put in test.php and i moved this topic because i t said i was bumping this topic(or something like that)
viewtopic.php?p=374379
Posted: Mon Apr 16, 2007 1:48 pm
by RobertGonzalez
I think you are going to need to take the php tag replacement out of the preg_replace and use a callback function instead just for that piece. The way it is working now, the function will not get parsed, it will get replaced as a string.
Posted: Mon Apr 16, 2007 1:49 pm
by ziggy3000
why did you lock my other post??
feyd pm'd me because he said that i was bumping
Posted: Mon Apr 16, 2007 1:54 pm
by ziggy3000
how do you use a callback function?? i am not that advanced into php. i dont know what you mean. i am a beginner->intermediate php coder. can you give me the code i need to use and i will try to search the php manual on how you did it.
Posted: Mon Apr 16, 2007 3:45 pm
by RobertGonzalez
ziggy3000 wrote:can you give me the code i need...
I gave you code already. I am not sure why it doesn't work for you.
As for locking the other thread, that was because it was a repeat of what was being discussed here. It was a duplicate of this thread content.
Posted: Mon Apr 16, 2007 5:15 pm
by ziggy3000
i am talking about the callback function thing. i dont know what that is. i looked on google and i found call_user_types() and other stuff like that. i know you gave me a working php bbcode function, but i like it when i code something, not when i just use what other people coded. and since i coded most of the stuff on that page(about 70%), it's okay with me if i use what other people code, but i like their code to be short. can you give me the lines of code that i need to make the bbcode test page work, and (if you want to) comment it so i understand it?
Posted: Mon Apr 16, 2007 5:22 pm
by John Cartwright
ziggy3000 wrote:i am talking about the callback function thing. i dont know what that is.
What you read
http://php.net/callback?
Posted: Mon Apr 16, 2007 5:24 pm
by ziggy3000
yeah thats what i read. but i dont get it. how is this supposed to help me?