मैं एक स्ट्रिंग का हिस्सा कैसे निकाल सकता हूं?
उदाहरण स्ट्रिंग: "REGISTER 11223344 here"
मैं "11223344"उपरोक्त उदाहरण स्ट्रिंग से कैसे निकाल सकता हूं ?
REGISTER hereडेटाबेस में क्यों बचाना चाहेगा ?
मैं एक स्ट्रिंग का हिस्सा कैसे निकाल सकता हूं?
उदाहरण स्ट्रिंग: "REGISTER 11223344 here"
मैं "11223344"उपरोक्त उदाहरण स्ट्रिंग से कैसे निकाल सकता हूं ?
REGISTER hereडेटाबेस में क्यों बचाना चाहेगा ?
जवाबों:
यदि आप विशेष रूप से "11223344" लक्ष्य कर रहे हैं, तो उपयोग करें str_replace:
// str_replace($search, $replace, $subject)
echo str_replace("11223344", "","REGISTER 11223344 here");
आप str_replace () का उपयोग कर सकते हैं , जिसे इस रूप में परिभाषित किया गया है:
str_replace($search, $replace, $subject)
इसलिए आप कोड इस प्रकार लिख सकते हैं:
$subject = 'REGISTER 11223344 here' ;
$search = '11223344' ;
$trimmed = str_replace($search, '', $subject) ;
echo $trimmed ;
यदि आपको नियमित अभिव्यक्ति के माध्यम से बेहतर मिलान की आवश्यकता है तो आप preg_replace () का उपयोग कर सकते हैं ।
11223344 मान लेना स्थिर नहीं है
$string="REGISTER 11223344 here";
$s = explode(" ",$string);
unset($s[1]);
$s = implode(" ",$s);
print "$s\n";
str_replace(find, replace, string, count)
ओपी उदाहरण के अनुसार:
$Example_string = "REGISTER 11223344 here";
$Example_string_PART_REMOVED = str_replace('11223344', '', $Example_string);
// will leave you with "REGISTER here"
// finally - clean up potential double spaces, beginning spaces or end spaces that may have resulted from removing the unwanted string
$Example_string_COMPLETED = trim(str_replace(' ', ' ', $Example_string_PART_REMOVED));
// trim() will remove any potential leading and trailing spaces - the additional 'str_replace()' will remove any potential double spaces
// will leave you with "REGISTER here"
जब आपको नियम आधारित मिलान की आवश्यकता होती है, तो आपको regex का उपयोग करने की आवश्यकता होती है
$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];
यह संख्या के पहले सेट से मेल खाएगा, इसलिए यदि आपको अधिक विशिष्ट प्रयास करने की आवश्यकता है:
$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];
$matchसरणी को फुलाता है । कोष्ठक निकालें, फिर के माध्यम से acces $match[0]।
substr () एक बिल्ट-इन php फंक्शन है जो एक स्ट्रिंग का हिस्सा देता है। फ़ंक्शन रूट () इनपुट के रूप में एक स्ट्रिंग लेगा, इंडेक्स फॉर्म जहां आप स्ट्रिंग को ट्रिम करना चाहते हैं। और एक वैकल्पिक पैरामीटर विकल्प की लंबाई है। आप http://php.net/manual/en/function.substr.php पर उचित दस्तावेज और उदाहरण कोड देख सकते हैं
नोट: एक स्ट्रिंग के लिए सूचकांक 0 से शुरू होता है।
डायनामिक रूप से, यदि आप किसी स्ट्रिंग के फिक्स्ड इंडेक्स (एस) से (ए) भाग (ए) को हटाना चाहते हैं, तो इस फ़ंक्शन का उपयोग करें:
/**
* Removes index/indexes from a string, using a delimiter.
*
* @param string $string
* @param int|int[] $index An index, or a list of indexes to be removed from string.
* @param string $delimiter
* @return string
* @todo Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
* types before each argument) and the return type.
*/
function removeFromString(string $string, $index, string $delimiter = " "): string
{
$stringParts = explode($delimiter, $string);
// Remove indexes from string parts
if (is_array($index)) {
foreach ($index as $i) {
unset($stringParts[(int)($i)]);
}
} else {
unset($stringParts[(int)($index)]);
}
// Join all parts together and return it
return implode($delimiter, $stringParts);
}
अपने उद्देश्य के लिए:
remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here
इसका एक उपयोग कमांड-जैसे स्ट्रिंग्स को निष्पादित करना है, जिसे आप उनकी संरचनाओं को जानते हैं।
निम्नलिखित स्निपेट "यहाँ पंजीकृत करें"
$string = "REGISTER 11223344 here";
$result = preg_replace(
array('/(\d+)/'),
array(''),
$string
);
print_r($result);
नीचे दिए गए अनुसार preg_replace () API हमारा उपयोग करता है।
$result = preg_replace(
array('/pattern1/', '/pattern2/'),
array('replace1', 'replace2'),
$input_string
);
preg_replace()इस मामले में सरणियों को पारित करने का कोई कारण नहीं है। न ही कब्जा समूह का उपयोग करने का कोई लाभ है।
निम्न कार्य करें
$string = 'REGISTER 11223344 here';
$content = preg_replace('/REGISTER(.*)here/','',$string);
यह "पंजीकरण" लौटाएगा
या
$string = 'REGISTER 11223344 here';
$content = preg_replace('/REGISTER (.*) here/','',$string);
यह "यहाँ पंजीकृत करें"
remove, लेकिन यह संभावना है कि आप इसका मतलबextractहै कि लक्षित स्ट्रिंग डेटाबेस में संग्रहीत किया जा सकता है।