2012-03-03 9 views
13

का उपयोग कर preg_replace_callback के लिए अतिरिक्त तर्क पास करना मैं इसी तरह के प्रश्नों का शोध कर रहा हूं, लेकिन मैं अभी भी थोड़ा अस्पष्ट हूं यदि यह संभव है और/या PHP 5.2.6PHP 5.2.6

का उपयोग कर preg_replace_callback में अतिरिक्त तर्क पारित करने का सबसे अच्छा तरीका है

इस मामले में मैं if_replace फ़ंक्शन के साथ फ़ोरैच लूप से $ कुंजी पास करने की भी तलाश कर रहा हूं।

public function output() { 
if (!file_exists($this->file)) { 
    return "Error loading template file ($this->file).<br />"; 
} 
$output = file_get_contents($this->file); 

foreach ($this->values as $key => $value) { 
    $tagToReplace = "[@$key]"; 
    $output = str_replace($tagToReplace, $value, $output); 
    $dynamic = preg_quote($key); 
    $pattern = '%\[if @'.$dynamic.'\](.*?)\[/if\]%'; // produces: %\[if @username\](.*?)\[/if\]% 
    $output = preg_replace_callback($pattern, array($this, 'if_replace'), $output); 
} 

return $output; 
} 



public function if_replace($matches) { 

    $matches[0] = preg_replace("%\[if @username\]%", "", $matches[0]); 
    $matches[0] = preg_replace("%\[/if]%", "", $matches[0]); 
    return $matches[0]; 
} 

अगर कुछ इस तरह काम करेगा सोच:

class Caller { 

public function if_replace($matches) { 

    $matches[0] = preg_replace("%\[if @username\]%", "", $matches[0]); 
    $matches[0] = preg_replace("%\[/if]%", "", $matches[0]); 
    return $matches[0]; 
} 

} 

$instance = new Caller; 

$output = preg_replace_callback($pattern, array($instance, 'if_replace'), $output); 
+0

Acallback फ़ंक्शन एक बंद है, आप उपयोग के माध्यम से अतिरिक्त तर्क पारित कर सकते हैं, कृपया http://stackoverflow.com/questions/16445991/how-do-i-access-a-variable-inside-of-preg पर उत्तर की जांच करें -replace-callback – Tester

उत्तर

28

पीएचपी 5.3 से पहले

आप सहायक वर्ग का उपयोग कर सकते हैं:

class MyCallback { 
    private $key; 

    function __construct($key) { 
     $this->key = $key; 
    } 

    public function callback($matches) { 
     return sprintf('%s-%s', reset($matches), $this->key); 
    } 
} 

$output = 'abca'; 
$pattern = '/a/'; 
$key = 'key'; 
$callback = new MyCallback($key); 
$output = preg_replace_callback($pattern, array($callback, 'callback'), $output); 
print $output; //prints: a-keybca-key 

के बाद से पीएचपी 5,3

आप गुमनाम समारोह का उपयोग कर सकते हैं:

$output = 'abca'; 
$pattern = '/a/'; 
$key = 'key'; 
$output = preg_replace_callback($pattern, function ($matches) use($key) { 
      return sprintf('%s-%s', reset($matches), $key); 
     }, $output); 
print $output; //prints: a-keybca-key 
+0

धन्यवाद, यह वही है जो मैं उम्मीद कर रहा था नमूना की सराहना करते हैं, यह बहुत स्पष्ट है और मैं इसे अनुकूलित करने में सक्षम था। – jsuissa

+1

जो भी इसे पढ़ता है: आप इस अपेक्षाकृत जटिल दृष्टिकोण के बजाय कीवर्ड 'उपयोग' का उपयोग करना चाहेंगे (देखें [@Mark बेकर द्वारा यह जवाब] (http://stackoverflow.com/a/16446110/722036) अधिक जानकारी के लिए) –

+0

@TheSexiestManinJamaica से ऊपर की टिप्पणी में उत्तर नई कक्षाओं आदि को जोड़ने से इस समस्या का एक बेहतर और सरल समाधान प्रदान करता है। – Chris

2

दुर्भाग्य से आप नहीं कर सकते। PHP 5.3 में आप केवल उन चरों तक पहुंच प्राप्त करने के लिए बंद करने का उपयोग कर सकते हैं जिन्हें आप पैरामीटर के रूप में पास करेंगे।

आपके मामले में दो संभावित समाधान हैं: एक साफ और गंदा वाला।

गंदे वैश्विक चर में पैरा को संग्रहीत कर रहे हैं ताकि आप उन्हें कॉलबैक के अंदर से एक्सेस कर सकें।

स्वच्छ एक कक्षा बना रहा है जहां आप पैराम पास करते हैं उदा। निर्माता के माध्यम से। फिर आप कॉलबैक के रूप में array($instance, 'methodName') का उपयोग करते हैं और बस अपनी विधि के अंदर $this->whatever के माध्यम से पैराम तक पहुंचते हैं।

+0

धन्यवाद कि मुझे सही दिशा में जाना है। मैंने आपकी टिप्पणी के आधार पर प्रश्न अपडेट किया, मुझे विश्वास है कि मैं समझता हूं कि आप क्या सुझाव दे रहे हैं। – jsuissa

16
$pattern = ''; 
$foo = 'some text'; 

return preg_replace_callback($pattern, function($match) use($foo) 
{ 
var_dump($foo); 

}, $content); 
+2

PHP 5.3 में काम करता है – Bald