String Replace a word that is between special characters [duplicate] - php

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 5 years ago.
$text_string = "One One One,One.One OneTwo, Onetwo .Onetwo TwoOne One";
I want to replace the word "One" with "Three" which is alone or between special characters or at the beginning/ending of the string.
Have anybody an idea?
The result must be:
$text_string = "Three Three Three,Three.Three OneTwo, Onetwo .Onetwo TwoOne Three";

You can use \b to check for word boundaries:
$str = 'One One One,One.One OneTwo, Onetwo .Onetwo';
$replaced = preg_replace('/\bOne\b/', 'Three', $str);
echo $replaced; // Three Three Three,Three.Three OneTwo, Onetwo .Onetwo TwoOne Three

Related

How to add a space between words? [duplicate]

This question already has answers here:
php explode at capital letters?
(4 answers)
Closed 1 year ago.
I have a string that has words without any spaces between them. How do I add a space between each word? Example: my string has combined words FinancialTimes and I would like it to show as Financial Times
<?php $word = 'FinancialTimes';?>
You could use preg_replace with a regex option:
$word = "FinancialTimes";
$output = preg_replace("/(?<=[a-z])(?=[A-Z])/", " ", $word);
echo $output; // Financial Times

Consecutive find/replace with regexp [duplicate]

This question already has answers here:
How to replace all occurrences of two substrings with str_replace()?
(3 answers)
Closed 5 years ago.
Say I want to erase any quote from this string, and then replace spaces with '&', in php. I could easily do with 2 consecutive preg_replace or the like, but how to do it in only 1 passage?
" columns="4" link="file" ids="280,281,282,283,284,285,286,287,288""
to:
columns=4&link=file&ids=280,281,282,283,284,285,286,287,288
You don't need regex for that. str_replace() should work fine:
$string = 'columns="4" link="file" ids="280,281,282,283,284,285,286,287,288"';
$replaced = str_replace([' ', '"'], ['&', ''], $string);
Demo: https://3v4l.org/gHHMp

Strip commas from a string in PHP [duplicate]

This question already has an answer here:
Cannot work out a php str_replace() to remove comma [closed]
(1 answer)
Closed 2 years ago.
I have the following string:
Input:
$str = "I want to remove only comma from this string, how ?";
I want to remove commas from $str, I'm new in programming and I don't understand how regex works.
use str_replace.
Example
$str = "I want to remove only comma from this string, how ?";
$str = str_replace(",", "", $str);
Explaination
As you can see there is 3 arguments we pass in str_replace
"," => this one is what you want to replace
"" => this one is value that will replace first argument value. we pass blank so it will replace comma to blank
this one is string where you want to replace.
Regex: (?<!\d)\,(?!\d)
(\,|\.) for matching exact either , or .
(?!\d) should not contains digits ahead.
(?<!\d) should not contains digit behind.
PHP code:
<?php
$str = "I want to remove only comma from this string, how. ? Here comma and dot 55,44,100.6 shouldn't be removed";
echo preg_replace("/(?<!\d)(\,|\.)(?!\d)/", "", $str);
Output:
I want to remove only comma from this string how ? Here comma 55,44,100 shouldn't be removed

Split string at an uppercase character [duplicate]

This question already has answers here:
Split camelCase word into words with php preg_match (Regular Expression)
(12 answers)
Closed 8 years ago.
My database contains full names, for example :
VernonClemans
SusanPostell
RonaldGutkowski
without any space or delimiter. I want to split them into first and last name
All full names have the same format: 1 word with 2 uppercase letters.
How can I split them?
<pre>
<?php
$string = "VernonClemans\nSusanPostell\nRonaldGutkowski";
foreach(explode("\n", $string) as $name) {
list($first_name, $last_name) = preg_split('/(?<=\\w)(?=[A-Z])/', $name);
print "First Name: $first_name \nLast Name: $last_name\n";
}
?>
</pre>

Keep only first 1000 occurrences of a words in a very long text [duplicate]

This question already has answers here:
Truncate string after certain number of substring occurrences in PHP? [duplicate]
(5 answers)
Closed 9 years ago.
How do I keep only the first 1000 occurrences of a words in a very long text and discard everything behind.
For example:
Sentence 1[ss]Sentence 2[ss]Sentence 3[ss]Sentence 4[ss]...Sentence 999[ss]Sentence 1000[ss]Sentence 1001[ss]Sentence 1002[ss]
Note: [ss] is my self-defined separator.
I would like to keep Sentence 1[ss]Sentence 2[ss]...Sentence 1000[ss]. In other words, I would like to keep EVERYTHING (including the [ss]) until the Sentence 1000[ss] and discard everything behind Sentence 1000[ss].
You can explode the string, slice the array and then implode it again:
$sentences = explode('[ss]', $string);
$sentences = array_slice($sentences,0, 1000);
$string = implode('[ss]', $sentences);
$array = explode('[ss]', $text);
array_splice($array, 1000);
$text = implode('[ss]', $array);

Categories