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.
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 = 'hello world!'; $search = ' '; $replace = '-'; 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)
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 = 'hello world!'; $replace = '-'; 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
