Home  >  Article  >  Backend Development  >  How to replace spaces in php

How to replace spaces in php

藏色散人
藏色散人Original
2020-11-05 11:28:082462browse

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:

 <?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=&#39;black&#39;>
$bodytag = str_replace("%body%", "black", "<body text=&#39;%body%&#39;>");
// 赋值: 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 = &#39;<br />&#39;;
// 首先替换 \r\n 字符,因此它们不会被两次转换
$newstr = str_replace($order, $replace, $str);
// 输出 F ,因为 A 被 B 替换,B 又被 C 替换,以此类推...
// 由于从左到右依次替换,最终 E 被 F 替换
$search  = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;);
$replace = array(&#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;);
$subject = &#39;A&#39;;
echo str_replace($search, $replace, $subject);
// 输出: apearpearle pear
// 由于上面提到的原因
$letters = array(&#39;a&#39;, &#39;p&#39;);
$fruit   = array(&#39;apple&#39;, &#39;pear&#39;);
$text    = &#39;a p&#39;;
$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!

Statement:
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