how to use regular expression for this below stuff in php - php

I have a database of more than 70000 records and its primary key value started from 1 single digit.
So I want user have to type nm0000001 instead of 1 in url.And in code part I have to discard the rest of the value except 1.
But my problem is i want this type of things having 9 letters in the string and the pattern is like this
1 - nm0000001
9 - nm0000009
10 - nm0000010
2020 - nm0002020
And from the above pattern i want only the digits like 1,9,10,2020 in php.

Here:
$i = (int)substr($input, 2);
No reason to use regexes at all.
Anyway, if you're insisting on using regexp, then:
$input = 'nm0002020';
preg_match('~0*(\d+)$~', $input, $matches);
var_dump($matches[1]);

Assuming the value is received in the URL as a querystring parameter, that is, passed via $_GET['id'] or some other name than id:
// Trim the "nm" off the front
$pk = substr($_GET['id'],2);
// And parse out an integer value.
$id = intval($pk);

There's absolutely no use for regular expressions in this -- use sprintf("nm%07d", ...) to format and just substr and a cast to int to parse.

This function will do the trick:
function extractID($pInput)
{
$Matches = array();
preg_match('/^nm0*(.*)$/', $pInput, $Matches);
return intval($Matches[1]);
}
Here's why /^nm0+(.*)$/ works:
The line must start (^) with exactly one nm
The pattern must continue with at least one 0
At the first non-zero character after nm0..., capture the value (that's the job of the parentheses)
Continue until the end of the line ($)

Related

check if array has this value and then grab it and place it an a variable

I have a string like the one below
20Nov 18:14:xxxxxxxxxx has given 10 points to xxxxx. New bitcoin collection Balance:XXXXXXXX. Ref:675743957424
I will explode it and it will then be turned into an array.
But I want to check if the array has Ref:675743957424 and then place it inside a variable like for example $a.
I want to do this since the string might change from one point to another so the position of Ref is not fixed.
How Can i obtain such thing?
Thanks.
Edited
I tried not exploding it but instead try grabbing the data see code below
<?php
$line = "20Nov 18:14:xxxxxxxxxx has given 10 points to xxxxx. New bitcoin collection Balance:XXXXXXXX. Ref:675743957424";
// perform a case-Insensitive search for the word "Vi"
if (preg_match("/\bRef\b/i", $line, $match)) :
print "Match found!";
//how can I grab the Ref part?
endif;
?>
You have to use:
preg_match ('/Ref:[\d]*/', $line, $matches);
The matches will be saved to variable $matches and then you can operate with said matches.
The RegExp, you just need to look for string Ref: followed by any amount of numbers (\d looks for any digit and * looks for zero or more ocurrences of the previous operator, digits in this case).
If you know the exact number of digits that you must to find and it is not varying you could use the pattern {NUMBER}, like:
preg_match ('/Ref:[\d]{12}/', $line, $matches);
This case, you are looking for 12 digits after Ref:.
You can use strpos() to check whether the substring present in the string. If it is true, you can assign that to your variabble. Pleas see the below code, it may help you.
$line = "20Nov 18:14:xxxxxxxxxx has given 10 points to xxxxx. New bitcoin collection Balance:XXXXXXXX. Ref:675743957424";
$string_to_check ='Ref:675743957424'
if (strpos($line,$string_to_check) !== false) { //Ref is present
$a = $line;
}

preg_match_all in php showing result blank

How do i match this with REGEXP and PHP ?
"s:6:\"[\"50\"]\";",
"s:5:\"[\"1\"]\";"
I want to match numbers between : [\"50\"] this only or could be one or more.
I have a pattern and want to take only numbers from json_encode value also serialize() in php this is code :
$result = [];
foreach($impressions as $impression) {
preg_match_all('/\x5C/', $impression->subcategories, $result);
}
return $result;
if no preg_match then here is result :
"s:6:\"[\"50\"]\";",
"s:5:\"[\"1\"]\";"
I am using this to match only digit where \ is so i can take number only like 50 or 1
Any idea how i can pic number with regular expressions ? value hex not works '/\x5C/' showing me result blank but here : Works fine if i put result and test with same REGEXP.
First of all, you can not go through an array of strings that way with preg_match_all – your $result array gets overwritten in each loop iteration.
And then, you need to capture the numbers you want to see in your result set. To do that, you must mask the [, ] and \ characters each with another \ – and then capture the digits in the middle by putting them in ( and )
$impressions[] = "s:6:\"[\"50\"]\";";
$impressions[] = "s:5:\"[\"1\"]\";";
foreach($impressions as $impression) {
preg_match_all('#\[\\"([0-9]+)\\"\]#', $impression, $matches); // I chose # as delimiter
// here – with so many \ involved, we don’t need / around it to add to the confusion
$results[] = $matches; // $matches will be overwritten in each iteration, so we
// preserve its content here by putting it into the $results array
}
var_dump($results);

php cut string from end of string

I have a string like any of the following:
$str = 'A001';
$str = 'B001';
$str = 'AB001';
$str = 'B0015';
....
$str = '001A';
I want to keep only 3 characters from the end of each string.
My code is like this:
$code = str_split($str);
$code = $code[1].$code[2].$code[3];
But it works for specific cases, but not for general ones! How I can get it for general ones?
I want to keep every 3 character from end of string
Simply Use substr
echo substr($str,-3); // Last 3 characters
Second parameter to this function is start, and according to the Manual
If start is negative, the returned string will start at the start'th character from the end of string.
Fiddle
Use sbstr()
echo substr($str,-3);//get last 3 char char
Or try:
echo $str[strlen($str)-3].$str[strlen($str)-2].$str[strlen($str)-1];
You need to use substr function.
All you need to do is to pass the string, and tell it where you cut the string off. If you want to cut the string off from end, you have to provide the value in negative.
substr($str, -3);
// The third argument is optional, which specifies the length of the returned string.

Regular expression to find a value and return it in PHP

I've been trying to figure this out for 2 hours now with no success. Its a bit complicated for me i guess.
I am trying to parse a script file in PHP and return some values to the user. The ones i want are like this:
_value = object runFunction blah blah blah
Basically what i want is (in an algorithm):
IF case-insensitive runFunction is found in the line (because it might be runfunction)
AND the line starts with _ (underscore) (or if possible before the = there is a value that starts with _ to be sure of the result)
THEN return that underscore value before the = to me.
Usually 99.9% the format is like this...But there are small cases it can be like this:
_value = _object runFunction blah blah blah
(in case the _ after the = messes things up).
Any help here :) ?
Thanks
try something like:
$str = 'YOUR FILE CONTENTS HERE';
$match = preg_match_all('/(_[a-zA-Z0-9_]+) ?= ?[a-zA-Z0-9_]+ runFunction/s',$str,$matches);
var_dump($matches);
you'll probably need to add the multiline flag.
How about
if (preg_match('/^_([^=]+?)(?=\s*=).*runfunction/im', $subject, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
You can exclude the initial "start" anchor is your underscore might not be at the beginning of the line
Here is the regex by itself. The results are in capturing group 1
^_([^=]+?)(?=\s*=).*runfunction
The regex
look for beginning of line
match the first underscore
capture everything that is not an '=' into capturing group 1
provided it is followed by 0 or more spaces and an equal sign.
then capture everything up to a runfunction.
Case insensitive and multiline options need to be set
If the first underscore does not need to be at the beginning of the line, eliminate the anchor.

php regex question - test 10 digits long with first 5 same digit and second 5 another

I'm trying to port this java to php:
String _value = '1111122222';
if (_value.matches("(1{5}|2{5}|3{5}|4{5}|5{5}|6{5}|7{5}|8{5}|9{5}){2}")) {
// check for number with the same first 5 and last 5 digits
return true;
}
As the comment suggests, I want to test for a string like '1111122222' or '5555566666'
How can I do this in PHP?
Thanks,
Scott
You can use preg_match to do so:
preg_match('/^(1{5}|2{5}|3{5}|4{5}|5{5}|6{5}|7{5}|8{5}|9{5}){2}$/', $_value)
This returns the number of matches (i.e. either 0 or 1) or false if there was an error. Since the String’s matches method returns only true if the whole string matches the given pattern but preg_match doesn’t (a substring suffices), you need to set markers for the start and the end of the string with ^ and $.
You can also use this shorter regular expression:
^(?:(\d)\1{4}){2}$
And if the second sequence of numbers needs to be different from the former, use this:
^(\d)\1{4}(?!\1)(\d)\2{4}$
Well, you could do:
$regex = '/(\d)\1{4}(\d)\2{4}/';
if (preg_match($regex, $value)) {
return true;
}
Which should be much more efficient (and readable) than the regex you posted...
Or, an even shorter (and potentially cleaner) regex:
$regex = '/((\d)\2{4}){2}/';
$f = substr($_value, 0, 5);
$s = substr($_value, -5);
return (substr_count($f, $f[0]) == 5 && substr_count($s, $s[0]) == 5);
Conversion is below. preg_match() is the key: http://www.php.net/preg_match
$value = '1111122222';
if (preg_match('/^(1{5}|2{5}|3{5}|4{5}|5{5}|6{5}|7{5}|8{5}|9{5}){2}$/', $value)) {
// check for number with the same first 5 and last 5 digits
return true;
}

Categories