2009-04-04 15 views
17
$split_point = ' - '; 
$string = 'this is my - string - and more'; 

मैं $ split_point के दूसरे उदाहरण का उपयोग करके एक विभाजन कैसे कर सकता हूं और पहले नहीं। क्या मैं किसी भी तरह से बाएं खोज का अधिकार निर्दिष्ट कर सकता हूं? सबसे अच्छा तरीका?बाएं दाएं स्ट्रिंग को कैसे विस्फोट करें?

मूल रूप से मैं दाएं से बाएं से कैसे विस्फोट कर सकता हूं। मैं "-" का अंतिम उदाहरण चुनना चाहता हूं।

परिणाम मैं की जरूरत है:

$item[0]='this is my - string'; $item[1]='and more'; 

और नहीं:

$item[0]='this is my'; $item[1]='string - and more'; 
+1

आप क्या प्राप्त करना चाहते हैं? – SilentGhost

+0

परिणाम: $ आइटम [0] = 'यह मेरा - स्ट्रिंग है'; $ आइटम [1] = 'और अधिक'; – Codex73

+0

आपकी सरणी को विस्फोट करने के बाद यह होगा: 'यह मेरा', 'स्ट्रिंग', 'और अधिक' –

उत्तर

26

आप strrev उपयोग कर सकते हैं स्ट्रिंग उल्टा करने के लिए, और फिर परिणामों को वापस रिवर्स:

$split_point = ' - '; 
$string = 'this is my - string - and more'; 

$result = array_map('strrev', explode($split_point, strrev($string))); 

सुनिश्चित नहीं हैं कि अगर यह सबसे अच्छा समाधान है, हालांकि है।

+1

क्रिएटिव है! उपाय! – Evert

+0

बहुत अच्छा लगता है, वाह ... मैं अब कोशिश करूँगा और देखूँगा। – Codex73

+0

विभाजन का उपयोग क्यों करें और विस्फोट न करें? कोई अंतर? – Codex73

4

अगर मैं सही ढंग से समझ, आप उदाहरण के मामले आप को देने के लिए ('यह मेरा है - स्ट्रिंग' चाहता हूँ 'और अधिक ')?

बिल्ट-इन स्प्लिट/विस्फोट केवल आगे प्रतीत होता है - आपको शायद इसे स्वयं को स्ट्रॉप्स के साथ लागू करना होगा। (दाएं-बाएं खोज)

$idx = strrpos($string, $split_point); 
$parts = array(substr($string, 0, $idx), substr($string, $idx+strlen($split_point))) 
2

क्यों '-' पर विभाजित नहीं है, लेकिन फिर पहले दो सरणी प्रविष्टियों में शामिल हों जो आप एक साथ वापस आते हैं?

+0

दिलचस्प लगता है। – Codex73

+0

लेकिन मुझे हमेशा के अंतिम उदाहरण पर विभाजित करने की आवश्यकता है -। – Codex73

+0

+1, सबसे आसान तरीका – SilentGhost

12

कैसे इस बारे में:

$parts = explode($split_point, $string); 
$last = array_pop($parts); 
$item = array(implode($split_point, $parts), $last); 

नहीं किसी भी गोल्फ पुरस्कार जीतने के लिए जा रहा है, लेकिन यह मंशा पता चलता है और अच्छी तरह से काम करता है, मुझे लगता है।

+0

के साथ उस स्थिति से Utf-8 तारों के साथ काम करता है (उदा: अरबी) –

0

मान लिया जाये कि आप केवल चाहते हैं $ split_point की पहली आवृत्ति पर ध्यान नहीं दिया जा रहा है, तो यह आपके लिए काम करना चाहिए:

# retrieve first $split_point position 
$first = strpos($string, $split_point); 
# retrieve second $split_point positon 
$second = strpos($string, $split_point, $first+strlen($split_point)); 
# extract from the second $split_point onwards (with $split_point) 
$substr = substr($string, $second); 

# explode $substr, first element should be empty 
$array = explode($split_point, $substr); 

# set first element as beginning of string to the second $split_point 
$array[0] = substr_replace($string, '', strpos($string, $substr)); 

यह आपको बाद $ split_point के हर घटना पर विभाजित करने की अनुमति देगा (और सहित) दूसरा $ split_point की घटना।

1

मैं एक ही uppon तो जैसे ठोकर खाई है, और तय यह:

$split_point = ' - '; 
$string = 'this is my - string - and more'; 

$reverse_explode = array_reverse(explode($split_point, $string)); 
5

यहाँ यह ऐसा करने का एक और तरीका है:

$split_point = ' - '; 
$string = 'this is my - string - and more'; 

$stringpos = strrpos($string, $split_point, -1); 
$finalstr = substr($string,0,$stringpos); 
0
$split_point = ' - '; 
$string = 'this is my - string - and more'; 
$result = end(explode($split_point, $string)); 

यह ठीक काम कर रहा है

1

मुझे माफ का जवाब पसंद आया, लेकिन मैंने तत्वों की संख्या को 2 तक सीमित करके और सरणी को फिर से उलटकर इसे सुधार दिया:

$split_point = ' - '; 
$string = 'this is my - string - and more'; 

$result = array_reverse(array_map('strrev', explode($split_point, strrev($string),2))); 

फिर $ परिणाम होगा:

Array ([0] => this is my - string [1] => and more) 
2

यह मेरा है - स्ट्रिंग - और अधिक

  1. उपयोग आम विस्फोट सभी स्ट्रिंग्स
  2. जाओ sizeof सरणी प्राप्त करने के लिए समारोह और अंतिम आइटम
  3. का उपयोग करके अंतिम आइटम पॉप करें rray_pop फ़ंक्शन।
  4. उसी डिलिमीटर के साथ शेष स्ट्रिंग को लागू करें (यदि आप अन्य डिलीमीटर का उपयोग कर सकते हैं)।

कोड

$arrSpits=explode("", "this is my - string - and more"); 
$arrSize=count($arrSpits); 

echo "Last string".$arrSpits[arrSize-1];//Output: and more 


array_pop(arrSpits); //now pop the last element from array 

$firstString=implode("-", arrSpits); 
echo "First String".firstString; //Output: this is my - string 
संबंधित मुद्दे