Home  >  Article  >  Backend Development  >  How to replace spaces with "-" in php

How to replace spaces with "-" in php

青灯夜游
青灯夜游Original
2021-02-20 11:43:512288browse

The str_ireplace() and str_replace() functions can be used in PHP to replace spaces with "-" characters; these two functions can use new strings to replace specific strings specified in the original string, and then Returns a string or array with replacement values.

How to replace spaces with

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php replace the spaces For "-"

In PHP, you can use str_ireplace() and str_replace to replace a specific string specified in the original string with a new string. str_replace is case-sensitive, str_ireplace() They are not case sensitive and have similar syntax.

str_ireplace() function

The syntax of str_ireplace() is as follows:

str_ireplace(find,replace,string,count)

Parameter description:

  • find Required. Specifies the value to look for.

  • replace Required. Specifies the value to replace the value in find .

  • #string Required. Specifies the string to be searched for.

  • #count Optional. A variable counting the number of substitutions.

This function returns a string or array with replacement values. This string or array is the result of replacing all search in subject with replace (ignoring case). The parameter count represents the number of times to perform the replacement.

Example:

<?php
$str = &#39;hello world!&#39;;
$search = &#39; &#39;;
$replace = &#39;-&#39;;
echo str_ireplace($search, $replace, $str);
?>

Output:

hello-world!

[Recommended learning: "PHP Video Tutorial"]

substr_replace () Function

substr_replace() The syntax of the function is as follows:

substr_replace(string,replacement,start,length)

How to replace spaces with - in php

substr_replace() In the copy of string string, it will be replaced by start and The substring qualified by the selected length parameter is replaced using replacement.

If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.

If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.

Return value: Returns the replaced string. If string is an array, the array is returned.

Example:

<?php
$str = &#39;hello world!&#39;;
$replace = &#39;-&#39;;
echo substr_replace($str, $replace, 5,1);
?>

Output:

hello-world!

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to replace spaces with "-" 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