How to add a space between words? [duplicate] - php

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

Related

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

How to allow specific values in my string [duplicate]

This question already has answers here:
Remove all special characters from a string [duplicate]
(3 answers)
Closed 5 years ago.
I want to replace space with "|" and i want only numbers and alphabets and star(*) in the sting
And i have tried str_replace() funtion but it replace the space with | , but i want to validate
echo $text= str_replace(' ','|', "This is some $123 Money");
output
This|is|some|$123|Money
what i expect is
This|is|some|123|Money
I don't want any other special characters in my output
Any Suggestions
Thanks in Advance
You can use preg_replace to remove all special characters from your string.
Example
echo $text= str_replace(' ','|', preg_replace('/[^A-Za-z0-9 ]/', '',"This is some $123 Money"));
Output
This|is|some|123|Money
Here is the working demo for you: https://3v4l.org/M456J
<?php
$s = "This is some $123 Money";
$result = preg_replace("/[^a-zA-Z0-9]+/", "|", $s);
echo $result;
Output:
This|is|some|123|Money

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);

Get rid of text in between () in PHP [duplicate]

This question already has answers here:
Remove Text Between Parentheses PHP
(7 answers)
Closed 9 years ago.
Say you have a string like this:
This is a string (with parenthesis stuff)
How would you change that to
This is a string
?
Replace it:
preg_replace('/\(.*?\)/', '', $str);
Use a regex.
Replace \([^)]*\) with the empty string.
Note: If there's more than one pair of parenthesis this will replace the innermost one.
try this
$string = "This is a string (with parenthesis stuff)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '
or you can do this also
$str = "This is a string (with parenthesis stuff)";
$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));

Categories