A simple problem.
I have the following string "48063974806397"
You will notice that this is just "4806397" repeated twice.
I need a way to recognize the repeat point, and just get the first instance of the pattern. E.g final return should just be "4806397".
(The length of the first number will not always be the same.)
I wanted to return this a variable in php.
How could I do this?
Thanks
If it's always just a string duplicated twice, then it's as simple as just taking the first half of the string:
$halfstring = substr($string, 0, strlen($string) / 2);
Use strlen() to get the length of the string, and divide that by 2. Then use substr() to just get the first half.
If that's always a number, math helps:
$halfStr = $n / (pow(10, strlen($n) / 2) + 1);
Related
I have a need to extract a sub-string from a longer string. I know how I would approach it using PHP posstr(); and strpos();, but the data is very large and I suspect that it would be more efficient if I could extract the part string using regex.
For example, if I have a number, (say a latitude) that has the format
"3203.79453"
where the the two characters before and "all" the characters after the decimal point represent decimal seconds, then to obtain the decimal latitude I need to compute the following:
32 + (03.79453)/60 = 32.06324217
So in essence I need a regex method of extracting the sub-string "03.79453".
So two questions how do I achieve it using regex and is it faster than using the method of using strpos() and posstr().
Thanks
It's easy to achieve with both options:
substr($line, strpos($line, '.') - 2);
or:
preg_match("/(\d{2}\..*)/", $line, $matches);
As for performance, I guess you would need to benchmark it. I've done a quick test to compare the performance of each example by running one million reps of each of those lines:
preg_match: average around 1.6 seconds for 1,000,000 matches
substr: average around 0.85 seconds for 1,000,000 matches
In this case it seems clear that using substr is the winner in terms of performance.
You could use preg_replace() like so:
<?php
$geoCoordinate = "3203.79453";
$degrees = preg_replace("#(\d{2}\.\d*?$)#", "", $geoCoordinate);
$seconds = preg_replace("#(\d*?)(\d{2}\.\d*?)#", "$2", $geoCoordinate);
$degAndSecs = round($degrees + ($seconds/60), 8);
var_dump($degAndSecs); //<== PRODUCES::: float 32.06324217
I have a script trimming strings this way billions of times.
$s = substr($s, 0, -$n);
Is there a way to do it faster an without reassigning the string?
By definition strings are not mutable in PHP. To "cut" a string, you'll have to create a new string based on the original string, making it necessary to reassign it. The code you have is probably already the most minimalist way to do it.
I would like to take this data:
questionX
Where X is any number from zero to infinity.
And subtract the text question from the field, and save just the number.
I was toying with substr($data, 0, 8) but haven't gotten it to work right, can somebody please give me a suggestion? Thanks so much.
The manual for substring http://php.net/manual/en/function.substr.php
indicates that the function takes three arguments: the string, the start, and the length. Length is optional. If omitted, substr will return the rest of the string starting from 'start'. One way to get your X value would be to do something like this:
$theNumber = substr($data, 8);
It look like you don't' really understand what substr does. Substr does not 'subtract' part of the string from itself. It returns the part of the string that you specify with the start and length paramaters
Try this
$number = str_replace("question", "", $string);
Users will be filling a field in with numbers relating to their account. Unfortunately, some users will have zeroes prefixed to the beginning of the number to make up a six digit number (e.g. 000123, 001234) and others won't (e.g. 123, 1234). I want to 'trim' the numbers from users that have been prefixed with zeros in front so if a user enters 000123, it will remove the zeroes to become 123.
I've had a look at trim and substr but I don't believe these will do the job?
You can use ltrim() and pass the characters that should be removed as second parameter:
$input = ltrim($input, '0');
// 000123 -> 123
ltrim only removes the specified characters (default white space) from the beginning (left side) of the string.
ltrim($usernumber, "0");
should do the job, according to the PHP Manual
$number = "004561";
$number = intval($number, 10);
$number = (string)$number; // if you want it to again be a string
You can always force PHP to parse this as an int. If you need to, you can convert it back to a string later
(int) "000123"
You can drop the leading zeros by converting from a string to a number and back again. For example:
$str = '000006767';
echo ''.+$str; // echo "6767"
Just multiply your number by zero.
$input=$input*1;
//000000123*1 = 123
I need to figure out how to do some C# code in php, and im not sure exactly how.
so first off i need the Split function, im going to have a string like
"identifier 82asdjka271akshjd18ajjd"
and i need to split the identifier word from the rest. so in C#, i used string.Split(new char{' '}); or something like that (working off the top of my head) and got two strings, the first word, and then the second part.. i understand that the php split function has been deprecated as of PHP 5.3.0.. so thats not an option, what are the alternatives?
and im also looking for a IndexOf function, so if i had the above code again as an example, i would need the location of 271 in the string, so i can generate a substring.
you can use explode for splitting and strpos for finding the index of one string inside another.
$a = "identifier 82asdjka271akshjd18ajjd";
$arr = explode(' ',$a); // split on space..to get an array of size 2.
$pos = strpos($arr[1],'271'); // search for '271' in the 2nd ele of array.
echo $pos; // prints 8