php substr and text manipulation - php

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);

Related

Why trim zeros from decimal part php not accurate for .00?

I have this float value 1290.00 and I would like to trim the zeros on the right in the cleanest way to get 1290, but Why I got 129 if using trim function?
Code:
<?php
$var = '1290.00';
printf ("value: %s -> %s\n", $var, trim($var, '.00'));
The output:
value: 1290.00 -> 129
I have seen different solutions to it by not using trim function, but why is trim not working? I also tried with GoLang and got the same behavior.
Trim removes characters from the end, individually, not as a phrase. It removes all found characters until it can't find any more in the list. So it removes the 0, the 0, the period, then the 0. In this case, I would recommend either round or number_format
number_format($var, 0, '', ''); // 1290
trim doesn't work like that. You only specify the characters you want to trim once, and two periods are used to specify a range. It will then trim all of those characters from the end.
A more efficient method might be intval($var)
Depending on your usage, note that number_format() also rounds the value (see the first example of the documentation). If you need to truncate the number you could use floor():
number_format(1.9, 0, '', ''); // string(1) "2"
floor(1.9); // 1

Truncate text to a desired end word

So I want to truncate a very long text but the problem is that I don't want x number of word or characters. I want the text truncated when it reaches a special string like ###end### or something similar e.g. I want to set where exactly it ends.
edit: I know how to check the existences of the string I wasn't sure how to make the truncation
Maybe something like:
$pos = strpos($mystring, "###end###");
$finalText = substr ( $mystring , 0 , $pos );
This can be done with a single call. My answer assumes that the targeted substring will exist in the string.
Code: (Demo)
$string='Here is a sample string.###end### I do not want to see any of this';
echo strstr($string,'###end###',true); // 3rd parameter 'true' will extract everything before
Output:
Here is a sample string.
If you are uncertain if the substring will exist (and if it doesn't, you want the fullstring) this is a reliable method:
Code: (Demo)
$string='Here is a sample string.###end### I do not want to see any of this';
echo explode('###end###',$string,2)[0]; // 3rd parameter 2 will limit the number of elements produced to two
Output:
Here is a sample string.

PHP String Pattern Task

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);

substring with last position in php

I want a substring from string in php with starting and last position instead of length.
Since we have function in php substr($str,$start_position,$length);
but I want $last_postion instead of $length because I don't know the length after starting position because it is variable.
e.g $str = october 8, 2012
$str = February 2, 2012
Try using this:
substr($str,$start_position,$last_postion-$start_position);
Well if you don't know the exact length, you need to use strpos first. http://php.net/manual/en/function.strpos.php
You use strpos($the_string, $the_string_to_find) to find the character you're looking for, then you use that returned value in $length for substr.
But as #john said, if you are trying to manipulate a date, or get some value from a date string, it would be a hundred times easier to use strtotime("october 8th, 2012"). You can then format that date however you want, or add / substract from it using a second multiplier.
substr($str, $start_position, $last_postion-$start_position)

Setting Substr to larger than string length

This is more of underlying PHP execution question.
What are the positives or negatives to the two following methods for limiting string size?
function max_length($string, $length) {
return (strlen($string) > $length)?substr($string, 0, $length):$string;
}
substr($string, 0, $length);
The case I am worried about is when the string is smaller than the length requested. Can it cause buffer overflow errors? Extra whitespace? Repeated characters?
The only thing I do know is that speed-wise, substr is at least 2x faster than the custom function provided.
As you can see in http://us2.php.net/substr
If length is given and is positive, the string returned will contain
at most length characters beginning from start (depending on the
length of string ).
That means you won't get any extra characters, just the ones you're asking for.
No, nothing to worry about. If the specified length argument is higher than the length of the string - the whole string, and nothing else - will be returned. So just go with:
$maxString = substr($string, 0, $length);

Categories