Gå til innhold

Sett farge på scriptene her på forumet


Anbefalte innlegg

Har laget et lite script som gjør det samme som highlight_string(), bare gjør det til bb-kode istede for html slik at det kan brukes på feks dette forumet ;)

 

Dette gjør koden mer oversiktelig, enn når den står i en code-boks...

 

Eksempel:

<?php

$enlitentest="Dette er en liten test"; //en test

echo $enlitentest;

?>

 

<?php

 

$enlitentest="Dette er en liten test";//en test

 

echo$enlitentest;

 

?>

 

Link til scriptet: http://www.nesjo.no/bbcode/

Endret av zokra
Lenke til kommentar
Videoannonse
Annonse

<?

/**

@param &$parent DOMElement Object - foreldrelement

@return DOMElement Object - selve fieldset elementet

*/

publicfunctionfieldset(&$parent,$title,$id=null,$class=null){

/*<fieldset>

<legend>Grupperingsboks</legend>

hehehe

</fieldset>*/

$fieldset=$parent->appendChild($this->doc->createElement('fieldset'));

$legend=$fieldset->appendChild($this->doc->createElement('legend'));

$legend->appendChild($this->doc->createTextNode(utf8_encode($title)));

if($id!=null){$fieldset->setAttribute("id",utf8_encode($id));}

if($class!=null){$fieldset->setAttribute("class",utf8_encode($class));}

return$fieldset;

}

?>

 

<?
/**
@param &$parent DOMElement Object - foreldrelement
@return DOMElement Object - selve fieldset elementet
*/
public function fieldset(&$parent,$title,$id=null,$class=null){
 /*<fieldset>
 <legend>Grupperingsboks</legend>
 hehehe
 </fieldset>*/	
 $fieldset=$parent->appendChild($this->doc->createElement('fieldset'));
 $legend=$fieldset->appendChild($this->doc->createElement('legend'));
 $legend->appendChild($this->doc->createTextNode(utf8_encode($title)));
 if($id != null){ $fieldset->setAttribute("id", utf8_encode($id));}
 if($class != null){ $fieldset->setAttribute("class", utf8_encode($class));}
 return $fieldset;
}
?>

 

Interessant, men det ser ut til at mellomrom forsvinner.

 

F.eks. "public function fieldset" ble til "publicfunctionfieldset"

Lenke til kommentar
Interessant, men det ser ut til at mellomrom forsvinner.

 

F.eks. "public function fieldset" ble til "publicfunctionfieldset"

Skal se på det ;)

 

EDIT: Fikset

Endret av zokra
Lenke til kommentar

Teste:

<?

/**

@param &$parent DOMElement Object - foreldrelement

@return DOMElement Object - selve fieldset elementet

*/

public function fieldset ( & $parent , $title , $id = null , $class = null ) {

/*<fieldset>

<legend>Grupperingsboks</legend>

hehehe

</fieldset>*/

$fieldset = $parent -> appendChild ( $this -> doc -> createElement ( 'fieldset' ) ) ;

$legend = $fieldset -> appendChild ( $this -> doc -> createElement ( 'legend' ) ) ;

$legend -> appendChild ( $this -> doc -> createTextNode ( utf8_encode ( $title ) ) ) ;

if ( $id != null ) { $fieldset -> setAttribute ( "id" , utf8_encode ( $id ) ) ; }

if ( $class != null ) { $fieldset -> setAttribute ( "class" , utf8_encode ( $class ) ) ; }

return $fieldset ;

}

?>

 

Edit: Joda, ser ut til å funke bra :)

Endret av Mellet
Lenke til kommentar
woops..nå ble det for mange mellomrom! :p

f.eks.

public function fieldset(&$parent,$title,$id=null,$class=null){

ble til

public function fieldset ( & $parent , $title , $id = null , $class = null ) {

Har sett det,det blir et mllomrom hver gang den skifter farge, skal se på det i kveld ;)

Lenke til kommentar
Gjest Slettet-df17e

Bra gjort, å flotters om folk begynner og bruke den. Så mye enklere og lese script da

Endret av Slettet-df17e
Lenke til kommentar
<?php

require_once ( dirname ( dirname ( __FILE__ ) ) . '/wp-config.php' ) ;

require_once ( ABSPATH . 'wp-includes/class-snoopy.php' ) ;

 

if ( ! get_option ( 'use_linksupdate' ) )

die ( 'Feature disabled.' ) ;

 

$link_uris = $wpdb -> get_col ( " SELECT link_url FROM $wpdb -> links " ) ;

 

if ( ! $link_uris )

die ( 'No links' ) ;

 

$link_uris = urlencode ( join ( $link_uris , "\\n" ) ) ;

 

$query_string = " uris = $link_uris " ;

 

$http_request = "POST /updated-batch/ HTTP/1.0\\r\\n" ;

$http_request .= "Host: api.pingomatic.com\\r\\n" ;

$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_settings ( 'blog_charset' ) . "\\r\\n" ;

$http_request .= 'Content-Length: ' . strlen ( $query_string ) . "\\r\\n" ;

$http_request .= 'User-Agent: WordPress/' . $wp_version . "\\r\\n" ;

$http_request .= "\\r\\n" ;

$http_request .= $query_string ;

 

$response = '' ;

if ( false !== ( $fs = fsockopen ( 'api.pingomatic.com' , 80 , $errno , $errstr , 5 ) ) ) {

fwrite ( $fs , $http_request ) ;

while ( ! feof ( $fs ) )

$response .= fgets ( $fs , 1160 ) ; // One TCP-IP packet

fclose ( $fs ) ;

 

$response = explode ( "\\r\\n\\r\\n" , $response , 2 ) ;

$body = trim ( $response [ 1 ] ) ;

$body = str_replace ( array ( "\\r\\n" , "\\r" ) , "\\n" , $body ) ;

 

$returns = explode ( "\\n" , $body ) ;

 

foreach ( $returns as $return ) :

$time = addslashes ( substr ( $return , 0 , 19 ) ) ;

$uri = addslashes ( preg_replace ( '/(.*?) | (.*?)/' , '$2' , $return ) ) ;

$wpdb -> query ( " UPDATE $wpdb -> links SET link_updated = ' $time ' WHERE link_url = ' $uri ' " ) ;

endforeach ;

}

?>

Siden BB-code ikke kan brukes i code-taggene, kan vel quote brukes...

 

EDIT: Indenteringen forsvinner, men det er vel ingenting vi kan gjøre med det.

 

EDIT2: Ueland jobber med en php-tag til forum v. 3 :w00t:

Endret av PT
Lenke til kommentar
Er det mulig å få se koden?

Det er et script jeg fant i en av kommentarene på manualen...

http://aidan.dotgeek.org/lib/?file=PHP_Highlight.php

<?php


if (!defined('T_ML_COMMENT')) {    define('T_ML_COMMENT', T_COMMENT); } if (!defined('T_DOC_COMMENT')) {     define('T_DOC_COMMENT', T_ML_COMMENT); } if (!defined('T_OLD_FUNCTION')) {     define('T_OLD_FUNCTION', -1); } if (!defined('T_ABSTRACT')) {     define('T_ABSTRACT', -1); } if (!defined('T_CATCH')) {     define('T_CATCH', -1); } if (!defined('T_FINAL')) {     define('T_FINAL', -1); } if (!defined('T_INSTANCEOF')) {     define('T_INSTANCEOF', -1); } if (!defined('T_PRIVATE')) {     define('T_PRIVATE', -1); } if (!defined('T_PROTECTED')) {     define('T_PROTECTED', -1); } if (!defined('T_PUBLIC')) {     define('T_PUBLIC', -1); } if (!defined('T_THROW')) {     define('T_THROW', -1); } if (!defined('T_TRY')) {     define('T_TRY', -1); } if (!defined('T_CLONE')) {     define('T_CLONE', -1); }


class PHP_Highlight
{
   /**
    * Hold the source
    *
    * @var         string
    * @access      private
    */
   var $_source = false;

   /**
    * Hold highlight colors
    *
    * Contains an associative array of token types and colours.
    * By default, it contains the colours as specified by php.ini
    *
    * For example, to change the colour of strings, use something
    * simular to $h->highlight['string'] = 'blue';
    *
    * @var         array
    * @access      public
    */
   var $highlight;

   /**
    * Things to be replaced for formatting or otherwise reasons
    *
    * The first element contains the match array, the second the replace
    * array.
    *
    * @var         array
    * @access      public
    */
   var $replace = array();

   /**
    * Format of the link to the PHP manual page
    *
    * @var         string
    * @access      public
    */
   var $manual = '[URL=http://www.php.net/function.%s]%s[/URL]';

   /**
    * Format of the span tag to be wrapped around each token
    *
    * @var         string
    * @access      public
    */
   var $span;

   /**
    * Constructor
    *
    * Populates highlight array
    *
    * @param   bool  $inline   If inline styles rather than colors are to be used
    */
   function PHP_Highlight($inline = true)
   {
       if ($inline === false) {
           // Default colours from php.ini
           $this->highlight = array(
               'string'    => ini_get('highlight.string'),
               'comment'   => ini_get('highlight.comment'),
               'keyword'   => ini_get('highlight.keyword'),
               'bg'        => ini_get('highlight.bg'),
               'default'   => ini_get('highlight.default'),
               'html'      => ini_get('highlight.html')
           );
           $this->span = '<span style="color: %s;">%s</span>';
       } else {
           // Basic styles
           $this->highlight = array(
               'string'    => 'red',
               'comment'   => 'orange',
               'keyword'   => 'green',
               'bg'        => 'white',
               'default'   => 'blue',
               'html'      => 'black'
           );
           $this->span = '[COLOR=%s]%s [/COLOR]';
       }
   }

 
   /**
    * Load a string
    *
    * @access  public
    * @param   string      $string     The string to load
    * @return  bool        Returns TRUE
    */
   function loadString($string)
   {
       $this->_source = $string;
       return true;
   }
 
   /**
    * Parse the loaded string into an array
    * Source is returned with the element key corresponding to the line number
    *
    * @access  public
    * @param   bool        $funcref        Reference functions to the PHP manual
    * @return  array       An array of highlighted source code
    */
   function toArray($funcref = true)
   {
       // Ensure source has been loaded
       if ($this->_source == false) {
           return false;
       }

       // Init
       $tokens     = token_get_all($this->_source);
       $manual     = $this->manual;
       $span       = $this->span;
       $i          = 0;
       $out        = array();
       $out[$i]    = '';

       // Loop through each token
       foreach ($tokens as $j => $token) {
           // Single char
           if (is_string($token)) {
               // Skip token2color check for speed
               $out[$i] .= sprintf($span, $this->highlight['keyword'], htmlspecialchars($token));

               // Heredocs behave strangely
               list($tb) = isset($tokens[$j - 1]) ? $tokens[$j - 1] : false;
               if ($tb == T_END_HEREDOC) {
                   $out[++$i] = '';
               }

               continue;
           }

           // Proper token
           list ($token, $value) = $token;

           // Make the value safe
           $value = htmlspecialchars($value);
           $value = str_replace($this->replace[0], $this->replace[1], $value);

           // Process
           if ($value === "\n") {
               // End this line and start the next
               $out[++$i] = '';
           } else {
               // Function linking
               if ($funcref === true && $token === T_STRING) {
                   // Look ahead 1, look ahead 2, and look behind 3
                   if ((isset($tokens[$j + 1]) && $tokens[$j + 1] === '(' ||
                       isset($tokens[$j + 2]) && $tokens[$j + 2] === '(') &&
                       isset($tokens[$j - 3][0]) && $tokens[$j - 3][0] !== T_FUNCTION
                       && function_exists($value)) {

                       // Insert the manual link
                       $value = sprintf($manual, $value, $value);
                   }
               }

               // Explode token block
               $lines = explode("\n", $value);              
               foreach ($lines as $jj => $line) {
                   $line = trim($line);
                   if ($line !== '') {
                       $out[$i] .= sprintf($span, $this->_token2color($token), $line);
                   }

                   // Start a new line
                   if (isset($lines[$jj + 1])) {
                       $out[++$i] = '';
                   }
               }
           }
       }

       return $out;
   }




   /**
    * Convert the source to formatted HTML.
    * Each line ends with <br />.
    *
    * @access  public
    * @param   bool      $return       Return rather than print the results
    * @param   bool      $linenum      Display line numbers
    * @param   string    $linenummod   Specify format of line numbers displayed
    * @param   bool      $funcref      Reference functions to the PHP manual
    * @return  string    A HTML block of code
    */
   function toHtml($return = true, $linenum = false, $linenummod = null, $funcref = true)
   {
       // Ensure source has been loaded
       if ($this->_source == false) {
           return false;
       }
       
       // Format code
       $source = $this->toArray($funcref);
       $out = "";
       foreach ($source as $i => $line) {
           $out .= "";
   

           $out .= $line;
           $out .= "\n";
       }
       $out .= "";

       if ($return === true) {
           return $out;
       } else {
           echo $out;
       }
   }


   /**
    * Assign a color based on the name of a token
    *
    * @access  private
    * @param   int     $token      The token
    * @return  string  The color of the token
    */
   function _token2color($token)
   {
       switch ($token):
           case T_CONSTANT_ENCAPSED_STRING:
               return $this->highlight['string'];
               break;

           case T_INLINE_HTML:
               return $this->highlight['html'];
               break;

           case T_COMMENT:
           case T_DOC_COMMENT:
           case T_ML_COMMENT:
               return $this->highlight['comment'];
               break;

           case T_ABSTRACT:
           case T_ARRAY:
           case T_ARRAY_CAST:
           case T_AS:
           case T_BOOLEAN_AND:
           case T_BOOLEAN_OR:
           case T_BOOL_CAST:
           case T_BREAK:
           case T_CASE:
           case T_CATCH:
           case T_CLASS:
           case T_CLONE:
           case T_CONCAT_EQUAL:
           case T_CONTINUE:
           case T_DEFAULT:
           case T_DOUBLE_ARROW:
           case T_DOUBLE_CAST:
           case T_ECHO:
           case T_ELSE:
           case T_ELSEIF:
           case T_EMPTY:
           case T_ENDDECLARE:
           case T_ENDFOR:
           case T_ENDFOREACH:
           case T_ENDIF:
           case T_ENDSWITCH:
           case T_ENDWHILE:
           case T_END_HEREDOC:
           case T_EXIT:
           case T_EXTENDS:
           case T_FINAL:
           case T_FOREACH:
           case T_FUNCTION:
           case T_GLOBAL:
           case T_IF:
           case T_INC:
           case T_INCLUDE:
           case T_INCLUDE_ONCE:
           case T_INSTANCEOF:
           case T_INT_CAST:
           case T_ISSET:
           case T_IS_EQUAL:
           case T_IS_IDENTICAL:
           case T_IS_NOT_IDENTICAL:
           case T_IS_SMALLER_OR_EQUAL:
           case T_NEW:
           case T_OBJECT_CAST:
           case T_OBJECT_OPERATOR:
           case T_PAAMAYIM_NEKUDOTAYIM:
           case T_PRIVATE:
           case T_PROTECTED:
           case T_PUBLIC:
           case T_REQUIRE:
           case T_REQUIRE_ONCE:
           case T_RETURN:
           case T_SL:
           case T_SL_EQUAL:
           case T_SR:
           case T_SR_EQUAL:
           case T_START_HEREDOC:
           case T_STATIC:
           case T_STRING_CAST:
           case T_THROW:
           case T_TRY:
           case T_UNSET_CAST:
           case T_VAR:
           case T_WHILE:
               return $this->highlight['keyword'];
               break;

           case T_CLOSE_TAG:
           case T_OPEN_TAG:
           case T_OPEN_TAG_WITH_ECHO:
           default:
               return $this->highlight['default'];

       endswitch;
   }

}





if (isset($_POST['kode'])) {	

$h = new PHP_Highlight;

$kode = $_POST['kode'];	
$kode = str_replace('\"', '"', $kode);
$kode = str_replace("\'", "'", $kode);

$h->loadString($kode);

echo '<textarea rows="30" cols="90" name="kode">';
$h->toHtml(false);
echo '</textarea>';	
}
else {
?>
Post kode her:
<form method="POST" action="index.php">

<textarea rows="30" cols="90" name="kode"></textarea>
<br>
<input type="submit" value="Send!">

</form>

<?php
}
?>

 

Ser at den legger til noen \ her og der... noen tips?

Lenke til kommentar
<?php

function CountFiles ( $dir , $type ) {

if ( ! ( $dh = @ opendir ( " $dir " ) ) )

return false ; // directory does not exist

 

// read the directory contents searching for "type" files

// and count how many are found:

$files = 0 ;

while ( ! ( ( $fn = readdir ( $dh ) ) === false ) ) {

$f = strrev ( $fn ) ;

$ext = substr ( $f , 0 , strpos ( $f , "." ) ) ;

$f_ext = strrev ( $ext ) ;

if ( ( strcasecmp ( $f_ext , $type ) == 0 ) ) $files ++ ;

}

closedir ( $dh ) ;

return $files ;

}

?>

 

Se der ja.. Fortjener en plass i favorittene den der :thumbup:

Endret av Loomy
Lenke til kommentar
Har laget et lite script som gjør det samme som highlight_string(), bare gjør det til bb-kode istede for html slik at det kan brukes på feks dette forumet ;)
Er det mulig å få se koden?

Det er et script jeg fant i en av kommentarene på manualen...

http://aidan.dotgeek.org/lib/?file=PHP_Highlight.php

Bestem deg... Har du laget det eller funnet det? ;)

Jeg har modifisert et script jeg har funnet, men startet å lage et eget ;)

Endret av zokra
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...