How to replace spaces in php

藏色散人
Release: 2023-03-07 10:54:02
Original
2459 people have browsed it

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.

How to replace spaces in php

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:


        
Copy after login

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 ] )
Copy after login

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

 $bodytag = str_replace("%body%", "black", ""); // 赋值: 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; ?>
Copy after login

Example #2 possible str_replace() replacement example

'; // 首先替换 \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; ?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!