I have a string, consisting of:
some numbers,
a single letter,
some numbers and letters.
For example: 987342j34kj3
I want to output something like:
$numbers = 987342
$letter = j
$restofit = 34kj3
Any idea how to do this with PHP? Can I use preg_match?
No problem:
preg_match('/(\d+)([a-z])([a-z0-9]+)/', $string, $matches);
// First element of $matches is the entire matched string, ignore it
list(, $numbers, $letter, $restofit) = $matches;
Related
I have a long string like below:
$string = "age1:32,age2:25,age3:52,..."
Now I want to extract the age numbers in this string and then add the numbers together and determine the average age.
I was able to extract the numbers with the help of the following code
$numbers = preg_replace('/[^0-9]/', '', $string );
but the output I get is like this and I cannot add the numbers together.
output:
322552
Using preg_match_all to find all age values followed by array_sum to find the total/average we can try:
$string = "age1:32,age2:25,age3:52,...";
preg_match_all("/age\d+:(\d+)/", $string, $matches);
print_r(array_sum($matches[1]) / count($matches[1])); // 36.333333333333
Restart the fullstring match after each colon, then match one or more digits.
preg_match_all() returns the number of matches ($count).
Divide the sum of the matches by the number of matches.
Code: (Demo)
$string = "age1:32,age2:25,age3:52,...";
$count = preg_match_all('/:\K\d+/', $string, $m);
echo array_sum($m[0] ?? []) / $count;
// 36.333333333333
If you wanted to grab all numbers, just use \d+. (Demo)
$string = "available12available15available22available11...";
$count = preg_match_all('/\d+/', $string, $m);
echo array_sum($m[0] ?? []) / $count;
I have a strings like this.
$str = "-=!#?Bob-Green_Smith";
$str = "-_#!?1241482";
How can I explode them at the first alphanumeric match.
eg:
$str = "-=!#?Bob-Green_Smith";
becomes:
$val[0] = "-=!#?";
$val[1] = "Bob-Green_Smith";
Quick thought some times the string won't contain the initial string of characters,
so I'd need to check if the first character is alphanumeric or not.. otherwise Bob-Green_Smith would get split when he shouldn't.
Thanks
You can use preg_match.
This will match "non word characters" zero or more as first group.
Then the rest as the second.
The output will have three items, the first is the full string, so I use array_shift to remove it.
$str = "-=!#?Bob-Green_Smith";
Preg_match("/(\W*)(.*)/", $str, $val);
Array_shift($val); // remove first item
Var_dump($val);
https://3v4l.org/m2MCg
You can do this like :
$str = "-=!#?1Bob-Green_Smith";
preg_match('~[a-z0-9]~i', $str, $match, PREG_OFFSET_CAPTURE);
echo $bubString = substr($str, $match[0][1]);
I have an alpha numeric string say for example,
abc123bcd , bdfnd567, dfd89ds.
I want to trim all the characters before the first appearance of any integer in the string.
My result should look like,
abc , bdfnd, dfd.
I am thinking of using substr. But not sure how to check for a string before first appearance of an integer.
You can easily remove the characters you don't want with preg_replace [docs] and a regular expression:
$str = preg_replace('#\d.*$#', '', $str);
\d matches a digit and .*$ matches any character until the end of the string.
Learn more about regular expressions: http://www.regular-expressions.info/.
DEMO
A possible non-Regex solution would be:
strcspn — Find length of initial segment not matching mask
substr — Return part of a string
Example:
$string = 'foo1bar';
echo substr($string, 0, strcspn($string, '1234567890')); // gives foo
$string = 'abc123bcd';
preg_replace("/[0-9]/", "", $string);
or
trim($string, '0123456789');
I believe you are looking for this?
$matches = array();
preg_match("/^[a-z]+/", "dfd89ds", $matches);
echo $matches[0]; // returns dfd
You can use a regex for this:
$string = 'abc123bcd';
preg_match('/^[a-zA-Z]*/i', $string, $matches);
var_dump($matches[0]);
will produce:
abc
To remove the +/- sign, you can simply use:
abs($number)
and get the absolute value.
e.g
$abs = abs($signed_integer);
I have a query string stored in a variable and I need to strip out some stuff from it using preg_replace()
the parameters I want to strip out look like this:
&filtered_features[48][]=491
As there will be multiples of these parameters in the query string the 48 and the 491 can be any number so the regex needs to essentially match this:
'&filtered_features[' + Any number + '][]=' + Any number
Anyone know how I would do this?
$string = '&filtered_features[48][]=491';
$string = preg_replace('/\[\d+\]\[\]=\d+/', '[][]=', $string);
echo $string;
I assume you wanted to remove the numbers from the string. This will match a multi-variable query string as well since it just looks for [A_NUMBER][]=A_NUMBER and changes it to [][]=
$query_string = "&filtered_features[48][]=491&filtered_features[49][]=492";
$lines = explode("&", $query_string);
$pattern = "/filtered_features\[([0-9]*)\]\[\]=([0-9]*)/";
foreach($lines as $line)
{
preg_match($pattern, $line, $m);
var_dump($m);
}
/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/'
this will match first number in n1 and second in n2
preg_match_all( '/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/', $str, $matches);
cryptic answer will replace more than necessary with this string:
&something[1][]=123&filtered_features[48][]=491
I am trying to filter out all characters before the first / sign. I have strings like
ABC/123/...
and I am trying to filter out ABC, 123 and ... into separate strings. I have alsmost succeeded with the parsing of the first letters before the / sign except that the / sign is part of the match, which I don´t want to.
<?php
$string = "ABC/123/...";
$pattern = '/.*?\//';
preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
The letters before the first/ can differ both in length and characters, so a string could also look like EEEE/1111/aaaa.
If you are trying to split the string using / as the delimiter, you can use explode.
$array = explode("/", $string);
And if you are looking only for the first element, you can use array_shift.
$array = array_shift(explode("/", $string));