I have a question about a String i want to count all the characters in the string. Like if i have a string
"Hello world & good morning. The date is 18.05.2016"
You can use explode() to convert string into array and then use count() function to count length of array.
echo count(explode(' ', "Hello world & good morning. The date is 18.05.2016"))
You can try this code.
<?php
$file = "C:\Users\singh\Desktop\Talkative Challenge\base_example.txt";
$document = file_get_contents($file);
$return_array = preg_split("/[\s,]+/",$document);
echo count($return_array);
echo $document;
?>
Hopefully it will be working fine.
The 3rd parameter of str_word_count allows you to set additional characters to be counted as words:
str_word_count($document, 0, '&.0..9');
&.0..9 means it will consider &, ., and range from 0 to 9.
You can count the spaces with substr_count and add one.
echo substr_count($str, " ")+1;
// 9
https://3v4l.org/oJJkt
Related
I have a STRING $special which is formatted like £130.00 and is also an ex TAX(VAT) price.
I need to strip the first char so i can run some simple addition.
$str= substr($special, 1, 0); // Strip first char '£'
echo $str ; // Echo Value to check its worked
$endPrice = (0.20*$str)+$str ; // Work out VAT
I don't receive any value when i echo on the second line ? Also would i then need to convert the string to an integer in order to run the addition ?
Thanks
Matt
+++ UPDATE
Thanks for your help with this, I took your code and added some of my own, There are more than likely nicer ways to do this but it works :) I found out that if the price was below 1000 would look like £130.00 if the price was a larger value it would include a break. ie £1,400.22.
$str = str_replace('£', '', $price);
$str2 = str_replace(',', '', $str);
$vatprice = (0.2 * $str2) + $str2;
$display_vat_price = sprintf('%0.2f', $vatprice);
echo "£";
echo $display_vat_price ;
echo " (Inc VAT)";
Thanks again, Matt
You cannot use substr the way you are using it currently. This is because you are trying to remove the £ char, which is a two-byte unicode character, but substr() isn't unicode safe. You can either use $str = substr($string, 2), or, better, str_replace() like this:
$string = '£130.00';
$str = str_replace('£', '', $string);
echo (0.2 * $str) + $str; // 156
Original answer
I'll keep this version as it still can give some insight. The answer would be OK if £ wouldn't be a 2byte unicode character. Knowing this, you can still use it but you need to start the sub-string at offset 2 instead of 1.
Your usage of substr is wrong. It should be:
$str = substr($special, 1);
Check the documentation the third param would be the length of the sub-string. You passed 0, therefore you got an empty string. If you omit the third param it will return the sub-string starting from the index given in the first param until the end of the original string.
I am trying to write a regular expression such that if a number have decimal point then the zeros (0) at the end must be removed.
Example:
$value = 234.8076000
After Regexp Replace it should become
234.8076
I am trying the following regexp [0]+$ in preg_replace but the problem is that if the value does not have decimal point and it contain zero at the end then that zero is also removed.
Example:
$value = 2340
It becomes 234 but it should remain 2340
Any idea? Is there any in-built function in php that can do this?
Yes, you really can do it with regex:
$pattern = '/(\.\d*[^0])0+$/';
echo preg_replace($pattern, '$1', '2340'); // 2340
echo preg_replace($pattern, '$1', '2340.0'); // 2340.0
echo preg_replace($pattern, '$1', '2340.07600'); // 2340.076
... but the simplest way is just convert a string value into a float value.
echo (float)'2340'; // 2340
echo (float)'2340.0'; // 2340
echo (float)'2340.07600'; // 2340.076
Echoing floats that are really integer values drops the decimal part apparently - but it seems from your comments it's actually what you want.
You can do it without regexpes:
php > echo ((float)"21.40200")."\n";
21.402
/(\.\d*?)0+$/, works except /\d+\.0{n}/ cases
I want to exclude a specific number like 4800 from a string of numbers like 569048004801.
I'm using php for this and the method preg_match_all some pattern's examples I have tried :
/([^4])([^8])([^0])([^0])/i
/([^4800])/i
If you just want to see if a string contain 4800, you don't need regular expressions :
<?php
$string = '569048004801';
if(strpos($string,'4800') === false){
echo '4800 was not found in the string';
}
else{
echo '4800 was found in the string';
}
More information about strpos in the documentation here
If you mean you simply want to remove 4800 from a string, this is easier with a str_replace:
$str = '569048004801';
$str = str_replace('4800', '', $str);
On the other hand, if you mean you want to know if a particular string of digits contains 4800, this will test that for you:
$str = '569048004801';
if (preg_match_all('/4800/', $str) > 0) {
echo 'String contains 4800.';
} else {
echo 'String does not contain 4800.';
}
/([^4])([^8])([^0])([^0])/i
This actually says, a sequence of four characters that is not "4800". Close.
/([^4800])/i
This actually says, a single character that is not '4', '8', or '0'.
Assuming you mean to capture a number that doesn't contain "4800" in it, I think you might want
/(?!\d*4800)\d+/i
This says, check first that we're not looking at a string of numbers with "4800" somewhere, and provided this is the case, capture the string of numbers. It's called a "negative lookahead assertion".
I'm trying to use strlen or something like that to count multiple set of characters
in a string.
Here's the database string
xa1xb2xcxd3xe4xcxa5xb6xcxd7xe8xcxa9xb10xcxd11xe12xcxa13xb14xc
I get the string from the database and i want to count the number of
"xa" "xb" "xd" and "xe" that the string contains.
I Guess i could count first xa then xb then xd then xe with strlen and then adding the numbers but isn't there an easier way?
Please help!
Are you open to preg_match_all solution?
echo preg_match_all('/x(a|b|d|e)/', $message, $matches)
From the docs:
Returns the number of full pattern matches (which might be zero), or
FALSE if an error occurred.
Although this returns the aggregated number of xa,xb,xd and xe, not the individual ones
If you need to get individual ones, you need one more step with array_count_values:
$sums = array_count_values($matches[0]);
print_r($sums);
Try this one.
$string ="xa1xb2xcxd3xe4xcxa5xb6xcxd7xe8xcxa9xb10xcxd11xe12xcxa13xb14xc";
echo substr_count($string, 'xa')."<br>";
echo substr_count($string, 'xb')."<br>";
echo substr_count($string, 'xc')."<br>";
echo substr_count($string, 'xd')."<br>";
http://php.net/manual/en/function.substr-count.php
$text = "xa1xb2xcxd3xe4xcxa5xb6xcxd7xe8xcxa9xb10xcxd11xe12xcxa13xb14xc";
echo substr_count($text, 'xa');
echo substr_count($text, 'xb');
echo substr_count($text, 'xd');
How can I add a new line characters (\n\r) in txt file every 10 characters?
What I have is a long sequence of characters, and I like to create a new line for each 10 characters.
in example, let's say I have that sequence of characters:
FadE4fh73d4F3fab5FnF4fbTKhuS591F60b55hsE
and I like to convert it to that:
FadE4fh73d
4F3fab5FnF
4fbTKhuS59
1F60b55hsE
How can I do that ?
I know that I can use a loop for that, but because the above string is an example and my string that I have to split it is really very very long, just I wander if there is any faster and more easy way to spit my string.
chunk_split($string, 10)
http://php.net/manual/en/function.chunk-split.php for more info
using chunk_split():
$str = chunk_split($str, 10, "\n\r");
or using this regex:
$str = preg_replace("/(.{10})/", "$1\n\r", $str);
And by the way did you mean \r\n (New line in Windows environment) by \n\r?
if so then the third argument for chunk_split() can be omitted.
<?php
$foo = '01234567890123456789012345678901234567890123456789012345678901234567890123456789';
$result = chunk_split ($foo, 10, "\r\n");
echo $result;
?>
As mentioned above, the use of chunk_split() might have unwanted consequences, as the break sequence is always added to the end once again.
You can instead use a combination of str_split() and implode() to first split the string every X characters and then recombine it with a break sequence. By using implode(), the break sequence will not be added to the end, again.
I've build a helper function who does this for me after 75 chars:
function createFold($s, $b = '\\n ') {
$chunks = str_split($s, 75);
return implode($b, $chunks);
}
<b><</b>?<b>php</b><br/>
$body=$row['details'];<br/>
$str = chunk_split($body, 14, "<b><</b><b>br</b><b>/</b>");<br/>
echo $str;<br/>
?