Man könnte so etwas tun:
Beispiel 1: Entfernen Sie die Leerzeichen, wenn mehr als 1
<?php
# Test variable
$string = "HELLO I HAVE WHITE SPACEEE!!1";
# Count all whitespaces
# Contains more than 1 whitespace
if(substr_count($string, ' ') > 1) {
# Example 1: Remove the whitespaces
$string = preg_replace('/\s+/', '', $string);
}
# Ouput: HELLOIHAVEWHITESPACEEE!!1
echo $string;
?>
Beispiel 2: Schneiden Sie die Whitespaces
<?php
# Test variable
$string = "HELLO I HAVE WHITE SPACEEE!!1";
# Count all whitespaces
# Contains more than 1 whitespace
if(substr_count($string, ' ') > 1) {
# Example 2: Remove the whitespaces
$string = trim($string);
}
# Ouput: HELLO I HAVE WHITE SPACEEE!!1
echo $string;
?>
Beispiel 3: Ersetzen Sie die Leerräume
<?php
# Test variable
$string = "HELLO I HAVE WHITE SPACEEE!!1";
# Count all whitespaces
# Contains more than 1 whitespace
if(substr_count($string, ' ') > 1) {
# Example 1: Replace the whitespaces
$string = preg_replace('/\s+/', ' ', $string);
}
# Ouput: HELLO I HAVE WHITE SPACEEE!!1
echo $string;
?>
Beispiel:
<?php
# Test variable
$string = "HELLO I HAVE WHITE SPACEEE!!1 And I like bananas.";
# Count all whitespaces
# Contains more than 1 whitespace
if(substr_count($string, ' ') > 1) {
# Example 1: Replace the whitespaces
$string = preg_replace('/\s\s+/', '', $string);
}
# Ouput: HELLOIHAVEWHITESPACEEE!!1 And I like bananas.
echo $string;
?>
Verwenden Sie 'trim (string);' Funktion –
Sie meinen, wie mit einer trim() Funktion? –
Wie gesagt ** Matt Magallo ** wenn der Inhalt in einer Variablen ist, verwenden Sie 'trim();' –