PHP strrchr parse int - php

I've got a problem. This is my PHP code :
$extract = $query;
$extractpoint = strrchr($extract, ".");
So, $extract is a parse_url of my website address.
Exemple : http://test.com?param.6
$extract = param.6 and $extractpoint = .6
BUT, I want a solution to have only the 6, without the point.
Can you help me with that ?

The easiest solution would be restructuring the URL. I that is not possible though you can use strpos to find the position of your specific character and then use substr to select the characters after it.
$extract = 'param.6';
echo substr($extract, strpos($extract, '.') + 1);
Demo: https://3v4l.org/CudTAG
(The +1 is because it returns the position of the match and you want to be one place past that)

There are different ways:
Filter only numbers:
$int = filter_var($extractpoint, FILTER_SANITIZE_NUMBER_INT);
Replace the point
$int = str_replace('.', '', $extractpoint)
//$int = str_replace('param.', '', $extractpoint)
Use regex
/[0-9+]/'

strrchr() results the count of the last instance of a character in a string. In order to get the next character add 1 to the count. Then use substr() to extract the next character from the string.
http://php.net/manual/en/function.strrchr.php
http://php.net/manual/en/function.substr.php

Related

How to cut only the FIRST character in a string

so I have this situation where I want to cut out the first '1.' out of a string, but not any following '1.'s. I am wondering if this is even possible to do.
So I am converting an it to a string, and I am wondering if there is a way to ONLY cut out the initial '1.' and not any following.
So my script dynamically assigns a number, for example 1, 1.1, 1.2, 2, 3, 3.1 - based on certain criteria. And it was currently adding 1. to the beginning of everything. So 1 would = 1.1, 2.1 would = 1.2.1 so on.
Is there a way to force it to ONLY take out the first and not any following? Here is my source:
$str = (string)$i; $str = $i;
$prepend = $parentPrepend ?
$parentPrepend . '.' . $i
: $str = ltrim($str, '\1');
$i++;
The reason your ltrim code doesn't work is that you are passing in \1 which is not the same as the character 1. \1 refers to the character whose ASCII code is 1 which is not the same as 1 whose ASCII code is actually \49.
Modify your code like this:
ltrim($str, '1');
That should trim all 1s from the left of the string.
However, you should know that the ltrim will remove all matching characters from the left of the string, not just the first one!
If you want only the first, then you should use substr instead, with a test to make sure it is a 1.
if(substr($str, 0, 1) == '1')
$str = substr($str, 1);
And if you want to remove the period too, then simply modify the code to include that (and look at first 2 characters instead of only first character)
if (strlen($str) > 2 && substr($str, 0, 2) == '1.')
$str = substr($str, 2);
use strpos to check if 1. is at the beginning. If it is, then use substr to return the string minus the 1.
$string = '1.1';
if (strpos($string, '1.') === 0) {
$string = substr($string, 2);
}
var_dump($string);
You could also use str_replace with a constraint:
$new_string = str_replace ('1.' , '' , $your_string, 1);

php - Get string before nth dash (-)

I have a string that looks something like this:
abc-def-ghi-jkl-mno-pqr-stu-vwx-yz I'd like to get the content BEFORE the 4th dash, so effectively, I'd like to get abc-def-ghi-jkl assigned to a new string, then I'd like to get mno assigned to a different string.
How could I go about doing this? I tried using explode but that changed it to an array and I didn't want to do it that way.
Try this:
$n = 4; //nth dash
$str = 'abc-def-ghi-jkl-mno-pqr-stu-vwx-yz';
$pieces = explode('-', $str);
$part1 = implode('-', array_slice($pieces, 0, $n));
$part2 = $pieces[$n];
echo $part1; //abc-def-ghi-jkl
echo $part2; //mno
See demo
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.implode.php
Can you add your source code? I done this one before but I cant remember the exact source code I used. But I am pretty sure I used explode and you can't avoid using array.
EDIT: Mark M answer is right.
you could try using substr as another possible solution
http://php.net/manual/en/function.substr.php
If I see where you are trying to get with this you could also go onto substr_replace
I guess an alternative to explode would be to find the position of the 4th - in the string and then get a substring from the start of the string up to that character.
You can find the position using a loop with the method explained at find the second occurrence of a char in a string php and then use substr(string,0,pos) to get the substring.
$string = "abc-def-ghi-jkl-mno-pqr-stu-vwx-yz";
$pos = -1;
for($i=0;$i<4;$i++)
$pos = strpos($string, '-', $pos+1);
echo substr($string, 0, $pos);
Code isn't tested but the process is easy to understand. You start at the first character (0), find a - and on the next loop you start at that position +1. The loop repeats it for a set number of times and then you get the substring from the start to that last - you found.

PHP : Get a number between 2 strings

I have this string:
a:3:{i:0;i:2;i:1;i:3;i:2;i:4;}
I want to get number between "a:" and ":{" that is "3".
I try to user substr and strpos but no success.
I'm newbie in regex , write this :
preg_match('/a:(.+?):{/', $v);
But its return me 1.
Thanks for any tips.
preg_match returns the number of matches, in your case 1 match.
To get the matches themselves, use the third parameter:
$matches = array();
preg_match(/'a:(\d+?):{/', $v, $matches);
That said, I think the string looks like a serialized array which you could deserialize with unserialize and then use count on the actual array (i.e. $a = count(unserialize($v));). Be careful with userprovided serialized strings though …
If you know that a: is always at the beginning of the string, the easiest way is:
$array = explode( ':', $string, 3 );
$number = $array[1];
You can use sscanfDocs to obtain the number from the string:
# Input:
$str = 'a:3:{i:0;i:2;i:1;i:3;i:2;i:4;}';
# Code:
sscanf($str, 'a:%d:', $number);
# Output:
echo $number; # 3
This is often more simple than using preg_match when you'd like to obtain a specific value from a string that follows a pattern.
preg_match() returns the number of times it finds a match, that's why. you need to add a third param. $matches in which it will store the matches.
You were not too far away with strpos() and substr()
$pos_start = strpos($str,'a:')+2;
$pos_end = strpos($str,':{')-2;
$result = substr($str,$pos_start,$pos_end);
preg_match only checks for appearance, it doesn't return any string.

Filter out numbers in a string in php

assuming i have these texts 'x34' , '150px' , '650dpi' , 'e3r4t5' ... how can i get only numbers ? i mean i want 34 , 150 , 650 , 345 without any other character . i mean get the numbers this string has into one variable .
$str = "e3r4t5";
$str_numbers_only = preg_replace("/[^\d]/", "", $str);
// $number = (int) $str;
Sorry for joining the bandwagon late, rather than using Regex, I would suggest you use PHP's built in functions, which may be faster than Regex.
filter_var
flags for the filters
e.g. to get just numbers from the given string
<?php
$a = '!a-b.c3#j+dk9.0$3e8`~]\]2';
$number = str_replace(['+', '-'], '', filter_var($a, FILTER_SANITIZE_NUMBER_INT));
// Output is 390382
?>
To adhere to more strict standards for your question, I have updated my answer to give a better result.
I have added str_replace, as FILTER_SANITIZE_NUMBER_FLOAT or INT flag will not strip + and - chars from the string, because they are part of PHP's exception rule.
Though it has made the filter bit long, but it's now has less chance of failing or giving you unexpected results, and this will be faster than REGEX.
Edit:
1: Realized that with FILTER_SANITIZE_NUMBER_FLOAT, PHP won't strip these characters optionally .,eE, hence to get just pure numbers kindly use FILTER_SANITIZE_NUMBER_INT
2: If you have a PHP version less than 5.4, then kindly use array('+', '-') instead of the short array syntax ['+', '-'].
You can use a regular expression to remove any character that is not a digit:
preg_replace('/\D/', '', $str)
Here the pattern \D describes any character that is not a digit (complement to \d).
Use PHP FILTER functions if you are using PHP 5.2.X, 5.3.x,5.4 . Its highly recommended
$mixed_input = "e3r4t5";
$only_numbers = filter_var($mixed_input, FILTER_SANITIZE_NUMBER_INT);
Please Go through with this link to know more
Replace everything that isn't a number and use that value.
$str = "foo1bar2baz3";
$num = intval(preg_replace("/[^0-9]/", "", $str));
You could use the following function:
function extract_numbers($string) {
preg_match_all('/([\d]+)/', $string, $match);
return $match;
}

splitting strings in php

I have some testcases/strings in this format:
o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data
o201_01_01b_Testing_to_see_If_this_testcases_passes:data
rx01_01_03d_Testing_the_reconfiguration/Retest:
Actually this testcase name consists of the actual name and the description.
So, I want to split them like this :
o201_01_01a Testing_to_see_If_this_testcases_passes:without_data
o201_01_01b Testing_to_see_If_this_testcases_passes:data
rx01_01_03d Testing_the_reconfiguration/Retest:
I am unable to figure out the exact way to do this in explode in php
Can anyone help please?
Thanks.
If the first part has always the same length, why don't you use substr, e.g.
$string = "o201_01_01a_Testing_to_see_If_this_testcases_passes:without_data";
$first_part = substr($string, 0, 11); // o201_01_01a
$second_part = substr($string, 12); // Testing_to_see_If_this_testcases_passes:without_data
$results = preg_split("/([a-z0-9]+_[0-9]+_[0-9]+[a-z])(.*)/", $input);
That should give you an array of results, provided I got the regular expression correct.
http://www.php.net/manual/en/function.preg-split.php
Looking at the pattern, it appears that you need to use regular expressions. If this is how they all are, you can cut off the beginning by looking for an upper case character. The code might look like this:
$matches = array()
preg_match('/^[^A-Z]*?/', $string, $matches);
$matches = substr($matches[0], 0, count($matches[0])-1);
Would put the first little part into $matches. Working on second part...

Categories