I have a string like this:
$myString = "line40";
Where the value 40 is variable (I have a set of random numbers).
I would like to get ONLY the number 40 (or any other random numer - 40 is just an example).
Just like this:
$newString = 40;
I tried to do using a regular expression but I failed... Please, someone can help me?
Thanks a lot.
$newString = preg_replace('/\D/', '', $myString);
The regular expression \D matches anything that isn't a digit. This replaces them all with nothing.
try preg_match()
$myString = 'line40';
preg_match('/([0-9])+/', $myString, $m);
echo $m[0]; // 40
How about this, You can use filters
$myString = "line40";
$myString = filter_var($myString, FILTER_SANITIZE_NUMBER_INT);
echo $myString; //output: 40
Ref: http://php.net/manual/en/filter.filters.sanitize.php
Related
I need to extract a project number out of a string. If the project number was fixed it would have been easy, however it can be either P.XXXXX, P XXXXX or PXXXXX.
Is there a simple function like preg_match that I could use? If so, what would my regular expression be?
There is indeed - if this is part of a larger string e.g. "The project (P.12345) is nearly done", you can use:
preg_match('/P[. ]?(\d{5})/',$str,$match);
$pnumber = $match[1];
Otherwise, if the string will always just be the P.12345 string, you can use:
preg_match('/\d{5}$/',$str,$match);
$pnumber = $match[0];
Though you may prefer the more explicit match of the top example.
Try this:
if (preg_match('#P[. ]?(\d{5})#', $project_number, $matches) {
$project_version = $matches[1];
}
Debuggex Demo
You said that project number is 4 of 5 digit length, so:
preg_match('/P[. ]?(\d{4,5})/', $tring, $m);
$project_number = $m[1];
Assuming you want to extract the XXXXX from the string and XXXXX are all integers, you can use the following.
preg_replace("/[^0-9]/", "", $string);
You can use the ^ or caret character inside square brackets to negate the expression. So in this instance it will replace anything that isn't a number with nothing.
I would use this kind of regex : /.*P[ .]?(\d+).*/
Here is a few test lines :
$string = 'This is the P123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
$string = 'This is the P.123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
$string = 'This is the P 123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
use explode() function to split those
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 need to validate strings like this:
$string = 'test3-10-2';
I need the penultimate number between hyphens, so in this case '10'. These are other examples:
$string2 = 'test45-50-178-1'; //match = 178
$string3 = 'test45-580-89-12-1'; //match = 12
Can you help me?
this RegExp should do the trick using a positive lookahead: (change 4 to whatever max num of digits you want to allow)
\d{1,4}(?=-\d{1,4}$)
You could also use the explode function (which is like a split):
$items = explode("-",$yourString);
end($items);
$whatYouWant = prev($items);
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
How can I get the value between the {} eg. 1 in "{1}" and use it?
if (preg_match('/\{(\d+)\}/', $str, $mtch))
echo $mtch[1];
where $str is '{1}'
If you just want to get rid of the brackets, you may use trim() function
$str = "{1}";
$str = trim($str, "{}");
echo $str; //output: 1
EDIT: I removed the comma - "{}" is enough as the secont parameter for trim() (was "{,}" before the edit)
You can use a preg_replace function to perform a regular expression.
What exactly do you need to do the string.
If you want to get a certain character of the string, $str = '{1}'; $str[1] will return the 2nd character of the string.
Depends on what you actually have .. in the example above this would be enough:
$number = $string{1};
But i guess you need more something like
preg_match('/{([0-9]+)}/', $string, $matches);
$number = $matches[1];