I am trying to extract substring from string gettting from mysql database using substr function:
substr($mystring,$startpoint,$endpoint);
here start and end point can be any number.
But I am not getting gesired result. Ptart point works but something is wrong with end point.
What is reason?
Edit
when I am pasin start ans end point like 15 and 50, start point is working fine so resultant string is starting from 15th char of main string . but end point is not working it's giving me meand end char in resultant string is not 50th of main string.
My guess is that you have mixed up endpoint with length. The third parameter of substring() is the length of the string - thus number of characters from the start point. Not the index of the last character.
<?php
$str = "A short string";
echo substr($str, 2, 2); // Prints sh
echo substr($str, 2, 4); // Prints shor
?>
If you want to specify an end point, you can calculate the length by subtracting the startpoint from your enpoint:
<?php
$startpoint = 2;
$endpoint = 5;
$str = "A short string";
echo substr($str, $startpoint, ($endpoint - $startpoint)); // Prints sho
?>
third parameter is for specifying length from start point.
But you want to get string till value in third parameter so both are different.
Try below it will work.
substr($string,$startpoint,($endpoint-$startpoint));
You should subtract $endpoint-$startpoint and pass it as third parameter to get desired output.
like below:
substr($mystring,$startpoint,($endpoint-$startpoint));
string substr ( string $string , int $start [, int $length ] )
length, not endpoint.
http://php.net/manual/en/function.substr.php
string substr (string $string, int $start [, int $length])
that means:
<?php
$text = "foobar text";
$startpoint = 0;
$endpoint = $startpoint + strlen($text);
echo substr($text, $startpoint, $endpoint);
?>
Related
i have a variable value which may contain
309
1208
31802
Now, i need to get 3/12/318. mean the rest part of the string after cutting last two string.how can i do it in php.
Do it like that:
substr($string, $start, $length)
substr($string, -3, (strlen($string)-2) * -1);
With this expression, you start at the third from last char of the string and select all the remaining chars to the beginning.
PHP-Documentation for substr: http://php.net/manual/de/function.substr.php
Another way is dividing by 100
<?php
echo (int)(309/100)."\n";
echo (int)(1208/100)."\n";
echo (int)(31802/100)."\n";
?>
https://eval.in/628062
I have a string like any of the following:
$str = 'A001';
$str = 'B001';
$str = 'AB001';
$str = 'B0015';
....
$str = '001A';
I want to keep only 3 characters from the end of each string.
My code is like this:
$code = str_split($str);
$code = $code[1].$code[2].$code[3];
But it works for specific cases, but not for general ones! How I can get it for general ones?
I want to keep every 3 character from end of string
Simply Use substr
echo substr($str,-3); // Last 3 characters
Second parameter to this function is start, and according to the Manual
If start is negative, the returned string will start at the start'th character from the end of string.
Fiddle
Use sbstr()
echo substr($str,-3);//get last 3 char char
Or try:
echo $str[strlen($str)-3].$str[strlen($str)-2].$str[strlen($str)-1];
You need to use substr function.
All you need to do is to pass the string, and tell it where you cut the string off. If you want to cut the string off from end, you have to provide the value in negative.
substr($str, -3);
// The third argument is optional, which specifies the length of the returned string.
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 receive data from a PUSH service. This data is compressed with gzcompress(). At the very Beginning of the data, it contains an int which is the length of the data contained. This is done after the gzcompress(); So a sample data would be:
187xœËHÍÉÉ,
Which is produced by
echo '187'.gzcompress('Hello');
Now, I don't know the length of the int, it could be 1 digit it could be 10 digits. I also don't know the first character to find the position of the beginning of a string.
Any ideas on how to retrieve/subtract the int?
$length_value=???
$string_value=???
Assuming that the compressed data would NEVER start with a digit, then a regex would be easiest:
$string = '187xœËHÍÉÉ,';
preg_match('/^(\d+)/', $string, $matches);
$number = $matches[0];
$compressed_data = substr($string, 0, strlen($number));
If the compressed data DOES start with a digit, then you're going to end up with corrupt data - you'll have absolutely no way of differentiating where the 'length' value stops and the compressed data starts, e.g.
$compressed = '123foo';
$length = '6';
$your_string = '6123foo';
Ok - is that a string of length 61, with compressed data 23foo? or 612 + 3foo?
You could use preg_match() to catch the integer at the start of the string.
http://php.net/manual/en/function.preg-match.php
You could do:
$contents = "187xœËHÍÉÉ,";
$length = (int)$contents;
$startingPosition = strlen((string)$length);
$original = gzuncompress(substr($contents, $startingPosition), $length);
But I feel this may fail if the first compressed byte is a number.
I got now a two sides that contains numbers and between two specific numbers there is a string that shows a group of numbers, Let's say we got this 123456789$numbers1234567 and I want to get the result of $numbers so how can I get it?
Thanks
If you know the two strings that it is sandwiched between then you can strip out the strings that you are looking for.
Not too elegant but this works:
$str1 = "123456789";
$str2 = "1234567";
$numberstr = "123456789";
$searchstring = "123456789".$numberstr."1234567";
$limit = 1;
$numbers = substr($searchstring, 0, strlen($searchstring) - strlen($str2)); // Remove the end of the string with length = $str2
$numbers = substr($numbers, strlen($numbers) - strlen($str1)); // Remove the most string from the beginning
print $numbers;
Output:
123456789
In summary, it removes the known string from the end, then the other known string from the beginning.
UPDATE: as per the comments, use two substrs to find the wanted string