Home >Backend Development >PHP Tutorial >How to Remove Multiple Whitespaces from MySQL Database Data?
Removing Multiple Whitespaces in MySQL Database Data
To remove multiple whitespaces (n, t, etc.) from MySQL database data, you can employ the following solutions:
Problem Explanation:
When retrieving data using $row['message'] from a MySQL database, you may encounter whitespace characters like n (newlines) and t (tabs) that can affect the formatting of your text. The goal is to eliminate these whitespaces while preserving single spaces.
Solution:
To accomplish this task, you can leverage the preg_replace() function with the following pattern:
$row['message'] = preg_replace('/\s+/', ' ', $row['message']);
Explanation:
Optimization:
For performance optimization, it's recommended to use the pattern ss * instead of ss . The former matches one or more whitespaces, while the latter matches two or more whitespaces, which is unnecessary in this scenario.
The above is the detailed content of How to Remove Multiple Whitespaces from MySQL Database Data?. For more information, please follow other related articles on the PHP Chinese website!