Split string at an uppercase character [duplicate] - php

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>

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

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

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

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

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

Trim multiple characters using php [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 12 months ago.
How to trim multiple characters at begin and end of string.
string should be something like {Hello {W}orld}.
i want to trim both { and } at begin and end.
don't want to use multiple trim function.
Use the optional second argument to trim which allows you to specify the list of characters to trim:
<?php
$str = "{Hello {W}orld}";
$str = trim($str, "{}");
echo "Trimmed: $str";
Output:
Trimmed: Hello {W}orld
Is there always a character at the beginning and at the end, that you want to remove? If so you could just use the following:
<?php
$string = '{Hello {W}orld}';
echo substr( $string, 1, strlen( $string )-2 );
?>
See: http://codepad.org/IDbG6Km2

Categories