$str = 'ABC300';
How I can get values like
$alphabets = "ABC";
$numbers = 333;
I have a idea , first remove numbers from the string and save in a variable. then remove alphabets from the $str variable and save. try the code
$str = 'ABC300';
$alf= trim(str_replace(range(0,9),'',$str));//removes number from the string
$number = preg_replace('/[A-Za-z]+/', '', $str);// removes alphabets from the string
echo $alf,$number;// your expected output
Try something like this (it's not that fast)...
$string = "ABCDE3883475";
$numbers = "";
$alphabets = "";
$strlen = strlen($string);
for($i = 0; $i <= $strlen; $i++) {
$char = substr($string, $i, 1);
if(is_numeric($char)) {
$numbers .= $char;
} else {
$alphabets .= $char;
}
}
Then all numbers should be in $numbers and all alphabetical characters should be in $alphabets ;)
https://3v4l.org/Xh4FR
A way to do that is to find all digits and use the array to replace original string with the digits inside.
For example
function extractDigits($string){
preg_match_all('/([\d]+)/', $string, $match);
return $match[0];
}
$str = 'abcd1234ab12';
$digitsArray = extractDigits($str);
$allAlphas = str_replace($digitsArray,'',$str);
$allDigits = '';
foreach($digitsArray as $digit){
$allDigits .= $digit;
}
Related
I need to move a specific character in a string from a specific position to right by specific places. string, position, and places are inputs it can be any value.
(Maximum use less number of lines using PHP functions).
$string = 'Peacock';
$position = 2;
$places = 2;
move($string,$position,$places);
function move($string,$position,$places){
$string[($position-1)+$places] = $string[$position-1];
echo $string ;
}
Expected output is Paceock
with for loop will be a good solution.
NB: the $position must be start from 1.
<?php
$string = 'Peacock';
$position = 2;
$places = 2;
move($string,$position,$places);
function move($string,$position,$places){
$keep = $string[$position-1];
for($i=0; $i<=$places;$i++){
$string[($position-1)+$i] = $string[$position+$i];
}
$string[$position+$places-1] = $keep;
echo $string ;
}
If you do not mind you can modify the string as well.
$string = 'Peacock';
$position = 2;
$places = 2;
function move($string,$position,$places){
$char = $string[$position-1];
$string = substr_replace($string, '', $position-1, 1);
$string = substr_replace($string, $char, $position+$places-1, 0);
echo $string;
}
move($string,$position,$places);
$string = 'blue*green-yellow-orange/rosa*white+lila';
$calcSigns = '+-*/';
$addstring = 'color1';
Whenever there is a calculation Sign I want to add after the calculation sign the string "color-1".
The result, that I am trying to achieve is:
blue*color-1green-color-1yellow-color-1orange/color-1rosa*color-1white+color-1lila
This is my approach:
$result = substr_replace($string, $addstring, $calcSigns);
But I do not get the correct result.
substr_replace() would make it tricky to preserve the current operation character while replacing. You could instead loop through each character and create a new string out of it.
<?php
$len = strlen( $string );
$new_string = '';
for( $i=0; $i<$len; ++$i ) {
$new_string .= $string[$i];
if( in_array( $string[$i], ['+','-','*','/'] ) ) {
$new_string .= $addstring;
}
}
echo $new_string;
?>
Demo: https://3v4l.org/P5tVr
Update:
So, if a operation character is immediately succeeded by a digit and if you want to skip it, and insert addString otherwise, it would look something like below:
<?php
$string = 'blue+yellow*3-grey+orange';
$calcSigns = '+-*/';
$addstring = 'color1';
$len = strlen($string);
$new_string = '';
for($i=0;$i<$len;++$i){
$new_string .= $string[$i];
if(in_array($string[$i],['+','-','*','/'])){
if($i + 1 < $len && is_numeric($string[$i + 1])) continue;
$new_string .= $addstring;
}
}
echo $new_string;
Demo: https://3v4l.org/uQobj
I have this function to put some random characters into a string:
function random($string) {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$shuffle_start = substr(str_shuffle($chars), 0, 6);
$shuffle_end = substr(str_shuffle($chars), 0, 6);
$letters = str_split($string);
$str = '';
$count = count($letters);
foreach($letters AS $l) {
$count--;
$str .= $l;
if($count) {
$str .= substr(str_shuffle($chars), 0, 5);
}
}
return $shuffle_start . $str . $shuffle_end;
}
This function prints this from the string "hello": aApi3VhKJrDjeAbCkalprX7ll7N0Qjo3qymiw. Now, I want to remove the random characters from the string so the word "hello" are being clearly seen.
How can I do this?
Just move backwards. Strip 6 characters form start and end, and then get every sixth character
function unrandom($str){
$base = substr($str, 6, strlen($str)-12);
$ret = '';
for($i=0;$i < strlen($base); $i+=6) {
$ret .= substr($base, $i,1);
}
return $ret;
}
I've got the problem, that I want to cut-off a long string after the fourth line-break and have it continue with "..."
<?php
$teststring = "asddsadsadsadsaa\n
asddsadsadsadsaa\n
asddsadsadsadsaa\n
asddsadsadsadsaa\n
asddsadsadsadsaa\n
asddsadsadsadsaa\n";
?>
should become:
<?php
$teststring = "asddsadsadsadsaa\n
asddsadsadsadsaa\n
asddsadsadsadsaa\n
asddsadsadsadsaa...";
?>
I know how to break the string after the first \n but I don't know how to do it after the fourth.
I hope you can help me.
you can explode the string and then take all the parts you need
$newStr = ""; // initialise the string
$arr = explode("\n", $teststring);
if(count($arr) > 4) { // you've got more than 4 line breaks
$arr = array_splice($arr, 0, 4); // reduce the lines to four
foreach($arr as $line) { $newStr .= $line; } // store them all in a string
$newStr .= "...";
} else {
$newStr = $teststring; // there was less or equal to four rows so to us it'all ok
}
echo preg_replace ('~((.*?\x0A){4}).*~s', '\\1...', $teststring);
Something like this ?
$teststring = "asddsadsadsadsaa
asddsadsadsadsaa
asddsadsadsadsaa
asddsadsadsadsaa
asddsadsadsadsaa
asddsadsadsadsaa";
$e = explode("\n", $teststring);
if (count($e) > 4)
{
$finalstring = "";
for ($i = 0; $i < 4; $i++)
{
$finalstring.= $e[$i];
}
}
else
{
$finalstring = $teststring;
}
echo "<pre>$finalstring</pre>";
How I cut the extra 0 string from those sample.
current string: 0102000306
required string: 12036
Here a 0 value have in front of each number. So, i need to cut the extra all zero[0] value from the string and get my expected string. It’s cannot possible using str_replace. Because then all the zero will be replaced. So, how do I do it?
Using a regex:
$result = preg_replace('#0(.)#', '\\1', '0102000306');
Result:
"12036"
Using array_reduce:
$string = array_reduce(str_split('0102000306', 2), function($v, $w) { return $v.$w[1]; });
Or array_map+implode:
implode('',array_map('intval',str_split('0102000306',2)));
$currentString = '0102000306';
$length = strlen($currentString);
$newString = '';
for ($i = 0; $i < $length; $i++) {
if (($i % 2) == 1) {
$newString .= $currentString{$i};
}
}
or
$currentString = '0102000306';
$tempArray = str_split($currentString,2);
$newString = '';
foreach($tempArray as $val) {
$newString .= substr($val,-1);
}
It's not particularly elegant but this should do what you want:
$old = '0102000306';
$new = '';
for ($i = 0; $i < strlen($old); $i += 2) {
$new .= $old[$i+1];
}
echo $new;