Home >Backend Development >PHP Problem >How to replace spaces in php
How to replace spaces in php: First create a PHP sample file; then use "str_replace("ll", "", "good golly miss molly!", $count); to remove the specified string of spaces.
Recommended: "PHP Video Tutorial"
php replace spaces (design ideas of php function)
1. Summary
1. To replace a sum, you need to search first and then replace. If you want to intercept, you need to search first and then intercept.
2. When designing a function, follow the steps The function is designed with the idea of default parameters last and core stuff first: when searching, $search is first (array_search(), str_search(), str_replace()), and when intercepting substrings, $arr is first ( substr(),str_slice)
2. Replace spaces with php
Title description:
Please implement a function to Replace spaces in a string with " ". For example, when the string is We Are Happy, the replaced string is We Are Happy.
Code:
<?php function replaceSpace($str) { return str_replace(" ","%20",$str); }
3. str_replace function
str_replace
(PHP 4, PHP 5, PHP 7)
str_replace — Substring replacement
Description
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
This function returns a string or array. This string or array is the result of replacing all search in subject with replace.
If there are no special replacement requirements (such as regular expressions), you should use this function to replace ereg_replace() and preg_replace().
Parameters
If search and replace are arrays, then str_replace() will perform mapping replacement on subject. If the number of replace values is less than the number of search values, additional replacements will be performed using empty strings. If search is an array and replace is a string, then the replacement of each element in search will always use this string. This conversion does not change case.
If search and replace are both arrays, their values will be processed in sequence.
search
The target value to find, which is needle. An array can specify multiple targets.
replace
Replacement value for search. An array can be used to specify multiple replacements.
subject
Array or string to perform replacement. That is haystack.
If subject is an array, the replacement operation will traverse the entire subject, and the return value will also be an array.
count
If specified, its value will be set to the number of times replacement occurred.
Return value
This function returns the replaced array or string.
Example
Example #1 str_replace() basic example
<?php // 赋值: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // 赋值: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); // 赋值: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); // 赋值: 2 $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; ?>
Example #2 possible str_replace() replacement example
<?php // 替换顺序 $str = "Line 1\nLine 2\rLine 3\r\nLine 4\n"; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; // 首先替换 \r\n 字符,因此它们不会被两次转换 $newstr = str_replace($order, $replace, $str); // 输出 F ,因为 A 被 B 替换,B 又被 C 替换,以此类推... // 由于从左到右依次替换,最终 E 被 F 替换 $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); // 输出: apearpearle pear // 由于上面提到的原因 $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text); echo $output; ?>
Comments
Note: This function is safe for use with binary objects.
The above is the detailed content of How to replace spaces in php. For more information, please follow other related articles on the PHP Chinese website!