General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 7th, 2003, 03:52 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Code for forums

i created a simple forums and i want to implament a php code like we have here with the [php] etc tags...any suggestions...or a place where i can find it?

Reply With Quote
  #2  
Old October 7th, 2003, 06:45 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
I'm pretty sure theres a function to syntax highlight php code which shouldn't be too hard to find on php.net then you can just do a strpos for the opening and closing tags and run the contents through that.

Hope that helps,

-KM-

Reply With Quote
  #3  
Old October 7th, 2003, 06:51 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
tried lookin and also applying what i found but no luck...i just want it to display a certain code like it is done in this forum

Reply With Quote
  #4  
Old October 8th, 2003, 02:45 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Ok then let me clarify a bit. Assuming for now that you're not parsing the string for anything else (which you're bound to be so you'll have to adapt this advice appropriately) the first task is to chop the posts up into three sections.

1 - The text before the php tag
2 - The text between the php and /php tags
3 - The text after the /php tag

The key function here being strpos

int strpos ( string haystack, string needle [, int offset])

So by making the haystack $post_text and the needle 'php' we can find out where that opening tag begins.

Similarly by keeping the haystack the same and making '/php' the needle we can find where the close tag begins.

The next function you'll want is substr.

string substr ( string string, int start [, int length])

You should be able to feed the post text and offsets strpos returned for you into here to break the post up into the three parts.

Having done that you can echo the first section to the screen, run the middle part through highlight_string which will syntax highlight it for you then echo the last part to the screen.

One quick note on highlight_string, it takes two arguments. The first is the string to be highlighted the second is a boolean value. If you set the second to true then it will return the highlighted string to you if not it echos it to the screen.

NB: If you want multiple sections of code in posts then you'll either want a while loop that keeps going until the strpos searching for 'php' returns false or some kind of regular expression.

Hope that helps,

-KM-

Further note: Throughout this post I've removed the square brackets surrounding php and /php to avoid this forum treating them as the start and end of code blocks. When you start coding don't forget to add them back in.

Last edited by kode_monkey : October 8th, 2003 at 02:47 AM.

Reply With Quote
  #5  
Old October 8th, 2003, 11:05 AM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
lost me after this string substr ( string string, int start [, int length])
...=[ ... i get the rest except the substr thing...thx though...will this be under one function? or more than one?

Last edited by Bruski : October 8th, 2003 at 11:11 AM.

Reply With Quote
  #6  
Old October 8th, 2003, 09:28 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
i tried making this code but it doesnt color code and it doesnt display most of it =[
PHP Code:
function BBCode($post)
        {
            
$post=nl2br($post);
            
// Declare the format for [code] layout
            
            // Check for [code] text
            
$post preg_replace("/\[code\](.+?)\[\/code\]/is",""$post);
            
highlight_string($post,true);
            return (
$post);
            
        } 

Reply With Quote
  #7  
Old October 9th, 2003, 02:30 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Sorry Bruski I'm not a regular expressions guy (ask anyone on my comp sci course and they'll tell you I frequently deny that they exist)

Using strpos and substr will work perfectly well for this but if you want to go down the regular expressions route then I'm afraid I'll have to pass you over to someone else.

Will keep monitoring this thread, if you have any more questions about the approach I suggested feel free to post them and I'll be happy to help,

-KM-

Reply With Quote
  #8  
Old October 9th, 2003, 06:57 AM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
how would i use substr to split the text ... i get the strpos

PHP Code:
 $post strpos $post"php")
$post strpos $post"/php"


im confused aout the substr because on php.net it talks about using integers to extract text...

thx for, really appreciate it

php should have brakets

Reply With Quote
  #9  
Old October 9th, 2003, 09:32 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
strpos returns the location of a given string within another string.

For example, if we're looking for the string 'needle' in the string 'hay needle stack' then we can use -

PHP Code:
 $offset strpos ('hay needle stack''needle'); 


In this case $offset will be set to 4 since thats where the string 'needle' starts in the other string.

If we give this offset to substr then we can extract all text from that point to the end of the string.

PHP Code:
 $sub_string substr ('hay needle stack'4); 


Having done this $sub_string will be set to 'needle stack'.

In your case you want to use strpos to find the offsets of 'php' and '/php' (remembering to add square brackets) then substr to extract the parts of the string and parse them appropriately.

If you're still unsure I recommend playing about with strpos and substr using different strings and arguments values. When you see them working its all far more obvious than in one off examples like this.

Hope this helps,

-KM-

Reply With Quote
  #10  
Old October 9th, 2003, 02:48 PM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
PHP Code:
function BBCode($post)
        {

$post strpos ('SELECT id, forumname''SELECT');
$post substr ('SELECT id, forumname'6);
$post highlight_string($post,true);
            return 
$post;
        } 


ran that to see if SELECT would highlight but...it doesnt

Reply With Quote
  #11  
Old October 10th, 2003, 04:43 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Ok here we go, I finally got around to just writing this for you rather than trying to explain. Hopefully this working example will push you in the right direction towards what you need.

PHP Code:
<?php
  
function BBCode ($post)
  {
    
$open_offset strpos ($post'php');
    
$close_offset strpos ($post'/php');
    
$code_offset $open_offset 5;
    
$end_offset $close_offset 6;
    
    
$open_section substr ($post0$open_offset);
    
$code_section substr ($post$code_offset$close_offset $code_offset);
    
$close_section substr ($post$end_offset);
    
    echo 
$open_section '<br />';
    
highlight_string ($code_section);
    echo 
'<br />' $close_section;
  }
  
  
$post 'Hello world this is some test text to start a post with.';
  
$post .= 'php<?php' "\n";
  
$post .= 'function BBCode ($post)' "\n";
  
$post .= "{\n";
  
$post .= '  $test_var = 7;' "\n";
  
$post .= '}?>/php';
  
$post .= 'And some more text to finish it off with.';

  
BBCode ($post);
?>


Although this works fine there are some problems with it that you'll need to adress but based on this you should be able to figure out how to do for yourself.

1 - The highlight_string function will NOT highlight unless the <? and ?> tags are present.

2 - It'll only work on the first section of code the rest will remain unhighlighted so you'll have to add a while loop in there to highlight all of them.

Hope this helps,

-KM-

NB: As always you'll need to add square brackets around each 'php' for this to work.

Reply With Quote
  #12  
Old October 10th, 2003, 11:32 AM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
thx =]

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Code for forums


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |