הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   [PHP] צריך עזרה ב highlight (https://hosts.co.il/forums/showthread.php?t=29704)

Elad-A 28-08-06 11:29

[PHP] צריך עזרה ב highlight
 
שלום,

אז ככה יש לי את הפונקצייה הבאה:

PHP קוד:


function highlight_php($string)
{
  
$Line explode("\n",$string);
  for(
$i=1;$i<=count($Line);$i++)
  {
 
$line .= "$i<br>";
  }
  
ob_start();
  
highlight_string($string);
  
$Code=ob_get_contents();
  
ob_end_clean();
  
$header='<table border="0" cellpadding="0" cellspacing="0" width="95%" style="border-style: solid; border-width:1px; border-color: white black black white">
   <tr>
     <td width="100%" colspan="2"  style="border-style: solid; border-width:1px; border-color: white; background-color: #99ccff; font-family:Arial; color:white; font-weight:bold;">PHP:</td>
   </tr>
   <tr>
     <td width="3%" valign="top" style="background-color: #99ccff; border-style: solid; border-width:1px; border-color: white;"><code>'
.$line.'</code></td>
     <td width="97%" valign="top" style="background-color: white;"><div style="white-space: nowrap; overflow: auto;"><code>'
;
  
$footer=$Code.'</div></code></td>
   </tr>
  </table>'
;
  return 
$header.$footer;


שבעעצם אמור לעשות מעין תגיות CODE כמו פה. אם אני רוצה ליצור תגיות אני עושה ככה:

PHP קוד:

$test="test \n test \n";
$active=highlight_php($test);
echo 
$active

עכשיו השאלה שלי איך אני יכול לעשות במקום לקרוא לפונקצייה ככה:

PHP קוד:

$test="test \n test \n";
$active=highlight_php($test);
echo 
$active

פשוט לעשות

PHP קוד:

[CODE] [*/CODE

תודה.
(אם לא הבנתם משהו אתה מוזמנים לשאול.)
ניתן לראות דוגמא פה: http://elad.fire-serv.net/1.php

RS324 28-08-06 11:42

אתה מדבר על 2 דברים שונים
בכל מקרה בשביל להחליף את CODE אתה צריך להשתמש ב PREG_REPLACE

תיכף אני יביא לך את הקוד המדוייק


בבקשה

תשתמש במה שאתה רוצה :

PHP קוד:

$patterns = array(

                                    
'`\[b\](.+?)\[/b\]`is',
                                    
'`\[i\](.+?)\[/i\]`is',
                                    
'`\[u\](.+?)\[/u\]`is',
                                    
'`\[strike\](.+?)\[/strike\]`is',
                                    
'`\[color=#([0-9]{6})\](.+?)\[/color\]`is',
                                    
'`\[email\](.+?)\[/email\]`is',
                                    
'`\[img\](.+?)\[/img\]`is',
                                    
'`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si',
                                    
'`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si',
                                    
'`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si',
                                    
'`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is',
                                    
'`\[quote\](.+?)\[/quote\]`is',
                                    
'`\[indent](.+?)\[/indent\]`is',
                                    
'`\[size=([1-6]+)\](.+?)\[/size\]`is'
                                

                                
);

$replaces =  array(

                                    
'<strong>\\1</strong>',
                                    
'<em>\\1</em>',
                                    
'<span style="border-bottom: 1px dotted">\\1</span>',
                                    
'<strike>\\1</strike>',
                                    
'<span style="color:#\1;">\2</span>',
                                    
'<a href="mailto:\1">\1</a>',
                                    
'<img src="\1" alt="" style="border:0px;" />',
                                    
'<a href="\1\2">\6</a>',
                                    
'<a href="\1\2">\1\2</a>',
                                    
'<a href="http://\1">\1</a>',
                                    
'<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>',
                                    
'<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#F7F7F7;border:1px dotted #CCCCCC;width:80%;"><em>\1</em></div>',
                                    
'<pre>\\1</pre>',
                                    
'<h\1>\2</h\1>'
                                    
                                    

                                    
);


$string preg_replace($patterns$replaces $string); 


Elad-A 28-08-06 11:46

זה בנוסף לקוד שלי?
עריכה:

לא נראה לי שהבנת אותי..
אני רוצה שבמקום לרשום
PHP קוד:

$test="test \n test \n"
$active=highlight_php($test); 
echo 
$active

אני פשוט ירשום

[CODE] טקסט [*/CODE]

הבנת?

-VladK- 28-08-06 14:17

אההההם השאלה שלך לא מובנת!...שניה בוא אני יחשוב קצת XD...

אוקי אז ככה....יש לך פונקציה מסוימת...אתה קורא לה בדרך מסוימת...אבל אתה לא רוצה לקרוא לה ככה...האם אני צודק?

אם כן אני חושש שאין מה לעשות...אלו הם החיים :(

RS324 28-08-06 15:31

ציטוט:

נכתב במקור על ידי Pilmen
אההההם השאלה שלך לא מובנת!...שניה בוא אני יחשוב קצת XD...

אוקי אז ככה....יש לך פונקציה מסוימת...אתה קורא לה בדרך מסוימת...אבל אתה לא רוצה לקרוא לה ככה...האם אני צודק?

אם כן אני חושש שאין מה לעשות...אלו הם החיים :(


אם ככה אתה מתכנת אז מצבך עגום

לגבי שואל השאלה ... זה אמור להיות לך משהו בסגנון הזה :

PHP קוד:

function highlight_php($string)
{
  
$Line explode("\n",$string);
  for(
$i=1;$i<=count($Line);$i++)
  {
 
$line .= "$i<br>";
  }
  
ob_start();
  
highlight_string($string);
  
$Code=ob_get_contents();
  
ob_end_clean();
  
$header='<table border="0" cellpadding="0" cellspacing="0" width="95%" style="border-style: solid; border-width:1px; border-color: white black black white">
   <tr>
     <td width="100%" colspan="2"  style="border-style: solid; border-width:1px; border-color: white; background-color: #99ccff; font-family:Arial; color:white; font-weight:bold;">PHP:</td>
   </tr>
   <tr>
     <td width="3%" valign="top" style="background-color: #99ccff; border-style: solid; border-width:1px; border-color: white;"><code>'
.$line.'</code></td>
     <td width="97%" valign="top" style="background-color: white;"><div style="white-space: nowrap; overflow: auto;"><code>'
;
  
$footer=$Code.'</div></code></td>
   </tr>
  </table>'
;
  return 
$header.$footer;


function 
bla($test)
{
 
return 
highlight_php($test); 
}

//replace [code] text [/code] from message

$message preg_replace('\[b\](.+?)\[/b\]',bla('\1'),$message); 


-VladK- 28-08-06 15:46

ציטוט:

נכתב במקור על ידי RS324
אם ככה אתה מתכנת אז מצבך עגום

לגבי שואל השאלה ... זה אמור להיות לך משהו בסגנון הזה :

PHP קוד:

function highlight_php($string)
{
  
$Line explode("\n",$string);
  for(
$i=1;$i<=count($Line);$i++)
  {
 
$line .= "$i<br>";
  }
  
ob_start();
  
highlight_string($string);
  
$Code=ob_get_contents();
  
ob_end_clean();
  
$header='<table border="0" cellpadding="0" cellspacing="0" width="95%" style="border-style: solid; border-width:1px; border-color: white black black white">
   <tr>
     <td width="100%" colspan="2"  style="border-style: solid; border-width:1px; border-color: white; background-color: #99ccff; font-family:Arial; color:white; font-weight:bold;">PHP:</td>
   </tr>
   <tr>
     <td width="3%" valign="top" style="background-color: #99ccff; border-style: solid; border-width:1px; border-color: white;"><code>'
.$line.'</code></td>
     <td width="97%" valign="top" style="background-color: white;"><div style="white-space: nowrap; overflow: auto;"><code>'
;
  
$footer=$Code.'</div></code></td>
   </tr>
  </table>'
;
  return 
$header.$footer;


function 
bla($test)
{
 
return 
highlight_php($test); 
}

//replace [code] text [/code] from message

$message preg_replace('\[b\](.+?)\[/b\]',bla('\1'),$message); 


חחח לא אני מכתנת ככה אני לא הבנתי את השאלה שלו ... חח ...

Elad-A 28-08-06 16:25

שגיאה:

preg_replace(): Delimiter must not be alphanumeric or backslash in /home/elad/domains/elad.fire-serv.net/public_html/1.php on line 34

ניסיתי ככה:

PHP קוד:


$header
='<table border="0" cellpadding="0" cellspacing="0" width="95%" style="border-style: solid; border-width:1px; border-color: white black black white"> 
   <tr> 
     <td width="100%" colspan="2"  style="border-style: solid; border-width:1px; border-color: white; background-color: #99ccff; font-family:Arial; color:white; font-weight:bold;">PHP:</td> 
   </tr> 
   <tr> 
     <td width="3%" valign="top" style="background-color: #99ccff; border-style: solid; border-width:1px; border-color: white;"><code>'
.$line.'</code></td> 
     <td width="97%" valign="top" style="background-color: white;"><div style="white-space: nowrap; overflow: auto;"><code>'
;
  
$footer=$t.'</div></code></td> 
   </tr> 
  </table>'

$t 'elad' ;
  
$t=str_replace("<code>""$header"$t);
   
$t=str_replace("</code>""$footer"$t); 

אבל לא עבד לי.. כאילו לא הציג את המבנה בכלל

RS324 28-08-06 16:46

תשתמש במה שאני הבאתי לך

צריך לעשות על זה כמה ניסיונות על השורה הזאת

ציטוט:

$message = preg_replace('\[b\](.+?)\[/b\]',bla('\1'),$message);
תבדוק..בשביל זה אתה לומד לתכנת...

Elad-A 28-08-06 16:53

עזוב אחי עשיתי משהו יותר טוב ויותר פשוט...

תראה פה:

http://elad.fire-serv.net/1.php

PHP קוד:


function PhpCode($t)
{
global 
$header,$footer;
   
$t=str_replace("[*php]""$header"$t);
   
$t=str_replace("[/*php]""$footer"$t);   
   return 
$t;
}
$x $_POST['text'];
$ea = <<<Elad
$x
Elad;
echo 
PhpCode("$ea");


עכשיו אני רק צריך לעשות כפתור שאם אני מסמן טקסט כלשהו זה ישים בתחילת הקוד שסימנתי את [PHP] ובסופו [/*PHP] אם תוכל לעזור לי אני ישמח

* הכוכביות זה רק בשביל שהקוד יראה פה נורמלי.

RS324 28-08-06 17:03

preg_replace עושה את העבודה הרבה יותר מהר והרבה יותר מקצועי
אבל כרצונך

לגבי הכפתור :

ב HEAD תשים את זה
PHP קוד:

<script type="text/javascript">
    function 
addtoForm(openTagcloseTagtextAreaId){ /// Add tags code to textarea
    
           
var txtAr document.getElementById(""+textAreaId), oRange
        txtAr
.focus()
        if (
document.selection && document.selection.createRange){
            
oRange=document.selection.createRange(); /// Create Range object form the selected Text
            
if (oRange.parentElement()==txtAr){ /// Check if the parent of the Range object is our textarea
             
oRange.textopenTag+oRange.text+closeTag /// add the tags before and after the selected text
            
}
        }else if(
navigator.appName=="Netscape"){
            var 
txtLength parseInt(txtAr.textLength// fint the length of the selected text
            
var selStart=txtAr.selectionStart /// find the start position
            
var selEnd=txtAr.selectionEnd /// find the end position
            
if (selEnd==|| selEnd==1){
                
selEnd=txtLength
            
}
            var 
sect1 = (txtAr.value).substring(0,selStart/// text before the selected text
            
var sect2 = (txtAr.value).substring(selStart,selEnd// the selected text
            
var sect3 = (txtAr.value).substring(selEnd,txtLength/// text after the selected text
            
txtAr.value sect1+openTag+sect2+closeTag+sect3 /// add the tags before and after the selected text
            
txtAr.focus()
        }
    }

</script> 

בכפתור תשים את זה :

[
PHP]
onclick=\"addtoForm('[p*h*p]','[/p*h*p]','formidhere')

[/PHP]


כל הזמנים הם GMT +2. הזמן כעת הוא 16:31.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ