Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Code colouring

    Right, I hope I am posting this in the right place. I assume I am cos I think I will need PHP RegEx.

    As on this board you get PHP code colouring. Is there a way that this could be done? Im not that good with regEx.

    Any help appreciated.

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP has a built in function for this.

    Have a look here.

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.

    Thanks for the link! That was alot easier than I was expecting!

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right, that works. Now, (this is why I thought RegExp) how do I make all text inside a [php] tag coloured in that way?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Depends on the time of year
    Posts
    478
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://www.php.net/manual/en/function.ereg.php would be the function you using.

    Could go something like this:

    [PHP]
    if (ereg('
    PHP Code:
    (.*)[/ php],$string))
    {
         
    highlight your code

    *Note, there is a gap between the / and php, that's just cause the format came out messed up if they were linked up together.

    Duniyadnd
    Last edited by duniyadnd; 04-16-2003 at 06:19 PM.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, that doesnt seem to work.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First, don't use the ereg functions, use the preg ones. 2nd, [ and ] should be escaped in a pattern, otherwise they dfine a character set
    [PHP]
    $someString = preg_replace( "/\\
    PHP Code:
    (.*)[/ php]/i", highlight_string( "$1" ), $someString ); 
    That should work

    Note: remove the space in [/ php]
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What is the variable 1, is that defined in the (.*) bit?

    PS: I knew the [] should be unescaped, so I tried that too.


    Thanks beetle, Ill give it a go.

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $1 is not a variable per se, but a backreference to the 1st matched parenthical in the pattern. So, everything matched by (.*) is retrieved in the replace argument with "$1"
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, still no luck.

    Here is my code

    PHP Code:

    $_POST
    ['code'] = preg_replace"/\[php\](.*)\[\/ php\]/i"highlight_string"$1" ), $_POST['code'] ); 

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have \\ behind all the [,], and /, But vB strips them out.

  • #12
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you write to a superglobal array like that? I'm not sure, but I think those are read-only. Use another variable to store the result
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #13
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I'll have another go thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •