[英]Find all php files that has more than 2 white spaces after the opening PHP tag
[英]How to remove all white spaces if more than one
我試圖從html內容中刪除PHP內容中的所有空格(如果長度超過一個),例如這樣的鏈:
{{IMG}} {{IMG}} {{IMG}} {{IMG}} {{IMG}}
但不應影響以下句子:關於我們。
應該用正則表達式完成嗎? 任何想法?
PS:該變量已與trim()一起使用; 它會刪除結尾和開頭的空格,但不會刪除字符之間的空格...
非常感謝你的幫助。
$str = preg_replace('/\\s+/', ' ', $originalString); echo $str;
這將用單個空格替換所有空格。
您可以執行以下操作:
示例1:如果大於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;
?>
示例2:修剪空白
<?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;
?>
示例3:替換空白
<?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;
?>
范例4:
<?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;
?>
如果您問我,這是一個很奇怪的要求。
您可能會做類似的事情(從接受的答案中采納,以防其他人發現它有用):
$str = preg_replace('/\s\s+/', '', $originalString);
如果連續至少有兩個空格,則會刪除所有空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.