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

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

RS324 23-09-06 10:49

1. איזה שגיאה זה מחזיר לך ?
2. זה יעבוד לך גם בלי מערכים..

-VladK- 23-09-06 10:58

Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement in an array. in /home/pilmen/domains/pilmen.sharvit.net/public_html/cms/index2.php on line 9

איך בדיוק?

RS324 23-09-06 11:11

החלטתי להפסיק לנסות לתת לך פתרונות בלי לבדוק

הבעיה אצלך היא ש PREG משתמש ב PLATERNS ו דברים שאתה רוצה להחליף
נראים ככה
<*CSS_NUM*>
וזה משנה את זה
אז אני יראה מה אפשר לעשות ויחזיר לך לפה תשובה עובדת...

-VladK- 23-09-06 11:19

תודה :) (מה זה PLATERNS?)

RS324 23-09-06 11:31

טוב אז ככה, לאחר כמה ניסיונות הגעתי למסקנה שיהיה קשה להשתמש ב PREG לכן החלטתי לבסוף להשתמש ב STR_REPLACE

זאת הפונקציה :

PHP קוד:

function str_tpl($dir,$file,$what,$with)
{
 
    
$tpl file_get_contents($dir."/".$file.".tpl");
 

    if (
is_array($what) AND is_array($with))
    {
        foreach (
$what AS $key=>$val)
        {
            
$tpl str_replace($what[$key],$with[$key],$tpl);
        }
        
    }
    else 
    {
        
$tpl str_replace($what,$with,$tpl);
    }
    
        return 
$tpl;


עכשיו דוגמאות להפעלה של הפונקציה :

PHP קוד:

$patterns = array();
$patterns[0] = '<*CSS_NUM*>';
$patterns[1] = '<*BLOCK_TITLE*>';
$patterns[2] = '<*BLOCKS*>';
$replacements = array();
$replacements[0] = 'this is css num';
$replacements[1] = 'כותרת';
$replacements[2] = 'block in here';
echo 
str_tpl('style','header',$patterns,$replacements); 

דוגמא נוספת :

PHP קוד:

echo str_tpl('style','header','<*CSS_NUM*>','mynum'); 

תהנה, עובד ב 100 %

-VladK- 23-09-06 15:08

ציטוט:

נכתב במקור על ידי RS324
טוב אז ככה, לאחר כמה ניסיונות הגעתי למסקנה שיהיה קשה להשתמש ב PREG לכן החלטתי לבסוף להשתמש ב STR_REPLACE

זאת הפונקציה :

PHP קוד:

function str_tpl($dir,$file,$what,$with)
{
 
    
$tpl file_get_contents($dir."/".$file.".tpl");
 

    if (
is_array($what) AND is_array($with))
    {
        foreach (
$what AS $key=>$val)
        {
            
$tpl str_replace($what[$key],$with[$key],$tpl);
        }
        
    }
    else 
    {
        
$tpl str_replace($what,$with,$tpl);
    }
    
        return 
$tpl;


עכשיו דוגמאות להפעלה של הפונקציה :

PHP קוד:

$patterns = array();
$patterns[0] = '<*CSS_NUM*>';
$patterns[1] = '<*BLOCK_TITLE*>';
$patterns[2] = '<*BLOCKS*>';
$replacements = array();
$replacements[0] = 'this is css num';
$replacements[1] = 'כותרת';
$replacements[2] = 'block in here';
echo 
str_tpl('style','header',$patterns,$replacements); 

דוגמא נוספת :

PHP קוד:

echo str_tpl('style','header','<*CSS_NUM*>','mynum'); 

תהנה, עובד ב 100 %

תודה רבה לך! מצטער שקצת שיגעתי לך תשכל ;P


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

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