How to count the number of characters after the last comma - php

I have a string similar to the following and would like to count the number of the characters after the last comma.
$test = "apple, orange, green, red";
$count = strlen($test);
echo "$count";
and it should return 3.
I have used the strlen command but it returns the length of the whole string.
Thank you for your help!

In this case you can use from following codes:
$test = "apple, orange, green, red";
$ex = explode(',',$test);
$ex = array_reverse($ex);
echo strlen(trim($ex[0]));
first convert your String to an array and reverse that and get length of 0 index.

<?php
$test = "apple, orange, green, red";
// Exploded string with ,(comman) and store as an array
$explodedString = explode(",", $test);
// with end() get last element
$objLast = end($explodedString);
// remove white space before and after string
$tempStr = trim($objLast);
//With strlen() get count of number of characters in string
$finalStringLen = strlen($tempStr);
print_r("Length of '".$tempStr."' is ".$finalStringLen);
?>

First, you have to explode the string with the comma(,) then store it into any variable. You have to pass the variable that you have used previously for storing exploded array value in END function because END function required any variable as a parameter. If you use END function and do something inside rather than passing the parameter you will get an error. After you have to trim value which is return from END function for removing useless space then after use strlen function for getting the exact count of last String.
$test = "apple, orange, green, red";
$t = explode(",",$test);
print_r(strlen(trim(end($t))));

$splitString = explode(',', $test);
echo strlen($splitString[count($splitString)-1]); //it will show length of characters of last part

Related

Getting STRING Characters value before a character occuring more than once in php

I want to get the value of the last word from a varaible.....
if the variable has a content of ,book,farm,chop,cook
i want to get the value of cook alone after the last , in php
$valt = ",book,farm,chop,cook";
or to get the value from the right hand side before a ',' is encountered...
Thanks
Use explode() to convert from string to array and use end().
<?php
$arr = ",book,farm,chop,cook";
$a = explode(',', $arr);
print_r(end($a));
Optional solution with strrpos which finds position of last occurence of ,. After that you can use substr to get a substring starting from the next position:
$str = ",book,farm,chop,cook";
print_r(substr($str, 1 + strrpos($str, ',')));
Using strrchr() and a ltrim() to tidy up you could do this
$valt = ",book,farm,chop,cook";
echo ltrim(strrchr($valt,','),',');
Result
cook
$valt = ",book,farm,chop,cook";
$r = explode(",",$valt);
$a = array_key_last($r);
print_r($a);

I want to add only integer values in a given string

<?php
$str = "1,2,3,4,b,6,c,7,8,f,9";
?>
I want to add only integer values in the above string... Can anyone suggest me an answer ? I tried explode function which will explode the character and return the integer values... But I failed to do so...
You need to split them up, filter anything that's not numeric out, then add them:
$chars = explode(',', $str);
$chars = array_filter($chars, 'is_numeric');
echo array_sum($chars); // 40

Remove characters from string based on user input

Suppose I have a string:
$str="1,3,6,4,0,5";
Now user inputs 3.
I want that to remove 3 from the above string such that above string should become:
$str_mod="1,6,4,0,5";
Is there any function to do the above?
You can split it up, remove the one you want then whack it back together:
$str = "1,3,6,4,0,5";
$userInput = 3;
$bits = explode(',', $str);
$result = array_diff($bits, array($userInput));
echo implode(',', $result); // 1,6,4,0,5
Bonus: Make $userInput an array at the definition to take multiple values out.
preg_replace('/\d[\D*]/','','1,2,3,4,5,6');
in place of \d just place your digit php
If you don't want to do string manipulations, you can split the string into multiple pieces, remove the ones you don't need, and join the components back:
$numberToDelete = 3;
$arr = explode(',',$string);
while(($idx = array_search($numberToDelete, $components)) !== false) {
unset($components[$idx]);
}
$string = implode(',', $components);
The above code will remove all occurrences of 3, if you want only the first one yo be removed you can replace the while by an if.

How do I know how many arguments explode created

Using explode(), How can I check how many arguments explode created? Is there function which check this or do I have to primary check how many times a character I chose to split on appears in string?
explode() return an array, the number of array elements can be returned with count().
$number = count(explode([a, b, c])); // 3
Return array after explode, use count() will do.
$str = 'Apple, Mango, Orange, Banana';
$exp = explode(',',$str);
echo count($exp);

How can we split a sentence

I have written the PHP code for getting some part of a given dynamic sentence, e.g. "this is a test sentence":
substr($sentence,0,12);
I get the output:
this is a te
But i need it stop as a full word instead of splitting a word:
this is a
How can I do that, remembering that $sentence isn't a fixed string (it could be anything)?
use wordwrap
If you're using PHP4, you can simply use split:
$resultArray = split($sentence, " ");
Every element of the array will be one word. Be careful with punctuation though.
explode would be the recommended method in PHP5:
$resultArray = explode(" ", $sentence);
first. use explode on space. Then, count each part + the total assembled string and if it doesn't go over the limit you concat it onto the string with a space.
Try using explode() function.
In your case:
$expl = explode(" ",$sentence);
You'll get your sentence in an array. First word will be $expl[0], second - $expl[1] and so on. To print it out on the screen use:
$n = 10 //words to print
for ($i=0;$i<=$n;$i++) {
print $expl[$i]." ";
}
Create a function that you can re-use at any time. This will look for the last space if the given string's length is greater than the amount of characters you want to trim.
function niceTrim($str, $trimLen) {
$strLen = strlen($str);
if ($strLen > $trimLen) {
$trimStr = substr($str, 0, $trimLen);
return substr($trimStr, 0, strrpos($trimStr, ' '));
}
return $str;
}
$sentence = "this is a test sentence";
echo niceTrim($sentence, 12);
This will print
this is a
as required.
Hope this is the solution you are looking for!
this is just psudo code not php,
char[] sentence="your_sentence";
string new_constructed_sentence="";
string word="";
for(i=0;i<your_limit;i++){
character=sentence[i];
if(character==' ') {new_constructed_sentence+=word;word="";continue}
word+=character;
}
new_constructed_sentence is what you want!!!

Categories