Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> PHP

PHP में String से सभी Spaces हटाना


PHP में string से spaces remove करने के कई तरीके हैं, आवश्यकता के अनुसार।

विधि 1: str_replace() - केवल Spaces (सबसे तेज़)

<?php
$str = "this is a test string";
$result = str_replace(' ', '', $str);
echo $result; // thisisateststring

विधि 2: strtr() - Multiple Characters

<?php
$str = "this is a test string";
// Array syntax: ['from' => 'to']
$result = strtr($str, [' ' => '', "\t" => '', "\n" => '']);
echo $result; // thisisateststring

विधि 3: preg_replace() - All Whitespace (Regex)

<?php
$str = "this\tis\na test\r\nstring";
// \s matches: space, tab, newline, carriage return, vertical tab, form feed
$result = preg_replace('/\s+/', '', $str);
echo $result; // thisisateststring

// या केवल spaces और tabs
$result = preg_replace('/[ \t]+/', '', $str);

विधि 4: str_split + implode (Character-level control)

<?php
$str = "this is a test";
// Filter out whitespace characters
$result = implode('', array_filter(str_split($str), function($char) {
    return !ctype_space($char);
}));
echo $result; // thisisatest

तुलना तालिका

विधिRemovesSpeedUse Case
str_replace(' ', '')केवल space ( )सबसे तेज़Simple space removal
strtr()Specified charsतेज़Multiple specific chars
preg_replace('/\s+/')सभी whitespaceधीमाComplete whitespace removal
preg_replace('/\h+/')Horizontal whitespaceधीमाSpaces + tabs only
preg_replace('/\v+/')Vertical whitespaceधीमाNewlines only

Whitespace Characters Reference

\s = [ \t\r\n\v\f]  // All whitespace
\h = [ \t]           // Horizontal (space, tab)
\v = [\r\n\v\f]      // Vertical (newlines, etc.)

व्यावहारिक उदाहरण

<?php
// 1. User input sanitization (keep single spaces between words)
function normalizeSpaces($str) {
    return preg_replace('/\s+/', ' ', trim($str));
}

// 2. Remove all whitespace for comparison
function compareIgnoreWhitespace($a, $b) {
    return preg_replace('/\s+/', '', $a) === preg_replace('/\s+/', '', $b);
}

// 3. CSS/JS minification (basic)
function minifyCss($css) {
    $css = preg_replace('/\s+/', ' ', $css);     // Collapse whitespace
    $css = preg_replace('/\s*([{}:;,])\s*/', '$1', $css); // Remove around punctuation
    return trim($css);
}

// 4. Phone number cleaning
function cleanPhone($phone) {
    return preg_replace('/\D/', '', $phone); // Keep only digits
}

महत्वपूर्ण नोट

trim() केवल string के शुरुआत और अंत से whitespace हटाता है, बीच से नहीं। बीच के spaces के लिए ऊपर दिए गए तरीके उपयोग करें।

  1. - पायथन में स्ट्रिंग से सभी विशेष वर्ण, विराम चिह्न और स्पेस कैसे हटाएं

    स्ट्रिंग से सभी विशेष वर्ण, विराम चिह्न और स्पेस हटाने के लिए, स्ट्रिंग पर पुनरावृत्ति करें और सभी नॉन-अल्फान्यूमेरिक वर्णों को फ़िल्टर करें। उदाहरण के लिए: >>> string = Hello $#! People Whitespace 7331 >>> .join(e for e in string if e.isalnum()) HelloPeopleWhitespace7331 नॉन-अल्

  1. - पायथन में स्ट्रिंग से सभी विराम चिह्न कैसे हटाएं?

    स्ट्रिंग से सभी विराम चिह्न हटाने का सबसे तेज़ तरीका str.translate() का उपयोग करना है। आप इसका उपयोग निम्न प्रकार से कर सकते हैं: import string s = string. With. Punctuation? print(s.translate(None, string.punctuation)) यह हमें आउटपुट देगा: string With Punctuation यदि आप अधिक पठनीय समाधान चाहते हैं,

  1. - पायथन स्ट्रिंग को स्पेस से कैसे भरें?

    स्ट्रिंग को दाईं ओर स्पेस या किसी अन्य कैरेक्टर से पैड करने के लिए आप str.ljust(width[, fillchar]) मेथड का उपयोग कर सकते हैं। fillchar आर्गु का उपयोग भरने के लिए कैरेक्टर निर्दिष्ट करने के लिए किया जाता है। उदाहरण के लिए: उदाहरण स्ट्रिंग को बाईं ओर स्पेस या किसी अन्य कैरेक्टर से पैड करने के लिए आप s