Get the solution ↓↓↓
As the title says
I want to insert or replace, a comma ( ,) into a string.
I have this string : A fits B fits C fits D. I want to insert comma behind every fits and right after the word before fits.
This is the desired result :A, fits B, Fits C, Fits D
The code I am using to achieve that is :
$newstr = substr_replace($oldstr,", ",stripos($oldstr,"fits")-1,0);
However, this code only insert 1 comma in the first occurrence of "fits".
I tried usingsubstr_count() to get the count of fits occurring and then usesFor loop but the comma is stacked in the position of the first occurrence of fits.
like this :A,,, Fits B Fits C Fits D
There must be a way to achieve the desired result, it has to be with adding more than one position insubstr_replace() function or something right?
EDIT
The string I have isWhite Fits Black Fits Red Fits Blue
The desired result isWhite, Fits Black, Fits Red, Fits Blue
The comma, is placed behind everyFits word in the string AND right after the word behindfits
the key point of my question is : How to put comma in behind everyfits word AND right after the word behindfits
Thanks before
Usepreg_replace:
$input = "A fits B Fits C Fits D";
$output = preg_replace("/\b([A-Z]+)(?=\s)/", "$1,", $input);
echo $input . "\n" . $output;
This prints:
A fits B Fits C Fits D
A, fits B, Fits C, Fits D
Here is an explanation of the regex pattern:
\b([A-Z]+) match AND capture one or more uppercase letters, which are preceded by a word boundary (?=\s) then assert that what follows is a space; this prevents a final letter from being assigned a comma, should the input end in a letter
Then, we replace with$1, the captured letter(s), followed by a comma.
Edit:
For your recent edit, you may use:
$input = "White Fits Black Fits Red Fits Blue";
$output = preg_replace("/\b(?=\s+Fits\b)/", ",", $input);
echo $input . "\n" . $output;
This prints:
White Fits Black Fits Red Fits Blue
White, Fits Black, Fits Red, Fits Blue
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.