I have a string like this "0011100001100111", and i would like to know the length of each sequence (00, 111, 0000, 11, 00, 111), in the right order.
How can I do that in PHP ?
Thanks to who will help me.
create a loop that loops through the entire string. It would look at each character in the string and compare it to the previous character. If it is the same, then it increases a counter, if it is different, it pushes the counter value onto an array and clears the counter.
Untested code:
function countSequenceLengths($str){
$counter = 1;
$lastChar = $str[0];
$ret = array();
for($i=1; $i<=strlen($str); $i++){
if($i<strlen($str) && $str[$i] == $lastChar){
$counter++;
}else{
$ret[] = $counter;
$counter = 1;
$lastChar = $str[$i];
}
}
return $ret;
}
You could use a regular expression to do that:
preg_split('/(?<=(.))(?!\\1)/', $str)
Here you’re getting an additional empty string at the end that you just need to remove:
array_slice(preg_split('/(?<=(.))(?!\\1)/', $str), 0, -1)
The code I made, with your help :
$string = "1111100111111001111110011111100111111001111... ...10011111100111111001";
$str = substr($string, 10, 12);
echo "str = '".$str."'<br />";
$array = array_slice(preg_split('/(?<=(.))(?!\\1)/', $str), 0, -1);
for($i=0; $i<count($array); $i++){
echo "array[".$i."] = '".$array[$i]."', ";
echo "array[".$i."] length = '".strlen($array[$i])."'<br />";
}
returns me the values I needed :
str = '111001111110'
array[0] = '111', array[0] length = '3'
array[1] = '00', array[1] length = '2'
array[2] = '111111', array[2] length = '6'
array[3] = '0', array[3] length = '1'
Thanks a lot !
Related
I want to iterate a string in the manner that after each character there should be a space and there will be new string(word) as per the main string character count.
For example
If I put the string "v40eb" as an input. Then Output be something like below.
v 40eb
v4 0eb
v40 eb
v40e b
OR
In Array form like below.
[0]=>v 40eb[1]=>v4 0eb[2]=>v40 eb[3]=>v40e b
I am using PHP.
Thanks
Well, you can divide the process of putting a space into 2 parts.
Get first part of the substring, append a space.
Get second part of the substring and join them together.
Use substr() to get a substring of a string.
Snippet:
<?php
$str = "v40eb";
$result = [];
$len = strlen($str);
for($i=0;$i<$len;++$i){
$part1 = substr($str,0,$i+1);
if($i < $len-1) $part1 .= " ";
$part2 = substr($str,$i+1);
$result[] = $part1 . $part2;
}
print_r($result);
Demo: https://3v4l.org/XGN0a
You could simply loop over the char positions and use substr to get the two parts for each:
$input = 'v40eb';
$combinations = [];
for ($charPos = 1, $charPosMax = strlen($input); $charPos < $charPosMax; $charPos++) {
$combinations[] = substr($input, 0, $charPos) . ' ' . substr($input, $charPos);
}
print_r($combinations);
Demo: https://3v4l.org/EeT1V
$input = 'v40eb';
for($i = 1; $i< strlen($input); $i++) {
$array = str_split($input);
array_splice($array, $i, 0, ' ');
$output[] = implode($array);
}
print_r($output);
Dont forget to check the codec. You might use mb_-prefix to use the multibyte-functions.
I have a string of delimited numerical values just like this:
5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004| ...etc.
Depending on the circumstance, the string may have only 1 value, 15 values, all the way up to 100s of values, all pipe delimited.
I need to count off (and keep/echo) the first 10 values and truncate everything else after that.
I've been looking at all the PHP string functions, but have been unsuccessful in finding a method to handle this directly.
Use explode() to separate the elements into an array, then you can slice off the first 10, and implode() them to create the new string.
$arr = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$a = explode ('|',$arr);
$b = array_slice($a,0,10);
$c = implode('|', $b);
Use PHP Explode function
$arr = explode("|",$str);
It will break complete string into an array.
EG: arr[0] = 5, arr[1] = 2288 .....
I would use explode to separate the string into an array then echo the first ten results like this
$string = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$arr = explode("|", $string);
for($i = 0; $i < 10; $i++){
echo $arr[$i];
}
Please try below code
$str = '5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324';
$arrayString = explode('|', $str);
$cnt = 0;
$finalVar = '';
foreach ($arrayString as $data) {
if ($cnt > 10) {
break;
}
$finalVar .= $data . '|';
$cnt++;
}
$finalVar = rtrim($finalVar, '|');
echo $finalVar;
I have this function:
function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
When I execute like this:
$arr = str_split_unicode('你好,我将于2014年11月11日变成霸王龙,这非常的好看呢!一二三四五六七八九!', 21);
echo '<pre>';print_r($arr);
The result is:
Array
(
[0] => 你好,我将于2014年11月11日变成霸王
[1] => 龙,这非常的好看呢!一二三四五六七八九!
)
This is not what I want. Because the Arabic number is count as 1 for 1, I hope to count 2 Arabic number as 1, how could I achieve this?
I'm going out on a limb here and will guess that you're actually trying to achieve a uniform block layout in which all characters line up in columns. For this purpose, you should rather use full width numerals ("zen-kaku") instead of trying to align them with 2-for-1:
echo mb_convert_kana('你好,我将于2014年11月11日变成霸王龙...', 'AS', 'UTF-8');
http://php.net/mb_convert_kana
I assume you wanted the number of numbers in the string
Could this be the answer?
$str = '你好,我将于2014年11月11日变成霸王龙,这非常的好看呢!一二三四五六七八九!';
preg_match_all("/\d+/", $str, $output_array);
echo count($output_array[0]); //number of digits in a string. in this case 3
if I have a string something like this:
$string = '01122028K,02122028M,03122028K,04122028M,05122028K,06122028P-2,07122028K,08122028P-';
How can I do to get the number of 'K' inside string $string. In this case K would be 4.
I know it can be solved by the help strpos() through out the looping after explode the $string into array. Is any php function to do it in straightforward way?
Thank you.
echo "There are " . substr_count($string, 'K') . " K's in the string";
If you don't want to count K-1 this can be:
echo "There are " . substr_count($string, 'K')-substr_count($string, 'K-') . " K's in the string";
To solve the new problem in the comments:
$string = '01122028K,02122028M,02122028K-1,02122028K-2,03122028K,04122028M,05122028K-1,04122028M,05122028K,06122028P-2,07122028K,08122028P-';
preg_match_all('/K(?:-\d+)?/', $string, $match);
$counts = array_count_values($match[0]);
print_r($counts);
Array
(
[K] => 4
[K-1] => 2
[K-2] => 1
)
Try also this solution:
$string = '01122028K,02122028M,03122028K,04122028M,05122028K,06122028P-2,07122028K,08122028P-';
$strlen = strlen($string);
$count = 0;
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $string, $i, 1 );
// $char contains the current character, so do your processing here
if ($char == "K")
$count++:
}
then...
echo $count;
That's not a "straightforward way" BUT I found it useful since it's very flexible and you can manipulate all sort of code on your string.
Good Luck!
I have a string I get from a website.
A portion of the string is "X2" I want to add +1 to 2.
The entire string I get is:
20120815_00_X2
What I want is to add the "X2" +1 until "20120815_00_X13"
You can do :
$string = '20120815_00_X2';
$concat = substr($string, 0, -1);
$num = (integer) substr($string, -1);
$incremented = $concat . ($num + 1);
echo $incremented;
For more informations about substr() see => documentation
You want to find the number at the end of your string and capture it, test for a maximum value of 12 and add one if that's the case, so your pattern would look something like:
/(\d+)$/ // get all digits at the end
and the whole expression:
$new = preg_replace('/(\d+)$/e', "($1 < 13) ? ($1 + 1) : $1", $original);
I have used the e modifier so that the replacement expression will be evaluated as php code.
See the working example at CodePad.
This solution works (no matter what the number after X is):
function myCustomAdd($string)
{
$original = $string;
$new = explode('_',$original);
$a = end($new);
$b = preg_replace("/[^0-9,.]/", "", $a);
$c = $b + 1;
$letters = preg_replace("/[^a-zA-Z,.]/", '', $a);
$d = $new[0].'_'.$new[1].'_'.$letters.$c;
return $d;
}
var_dump(myCustomAdd("20120815_00_X13"));
Output:
string(15) "20120815_00_X14"