Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am fetching data from a database as a string like 02-feb-1990 I want to remove all the characters from the string, and have only date of year means 1990.
How can I achieve this in PHP using regex or any other method
This should work for you:
<?php
$input = "02-feb-1990";
echo $year = substr($input, -4);
?>
Output:
1990
If you want to use regex:
<?php
$input = "02-feb-1990";
preg_match_all("/\d{4}$/", $input, $matches);
print_r($matches);
?>
Output:
Array ( [0] => Array ( [0] => 1990 ) )
In order to retrieve a 4 digit year from dd-mmm-yyyy using regex, you can use the following:
$date = "02-feb-1990"
preg_match('/\d{4}/', $date, $year);
echo $year[0];
The above will match the first 4 digit number you come across, which in this instance, will always be the 4-digit year.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to retrive a selected part from a url . I've used substr method and i've successfully get the character. But my issue is that ,this is my sample url localhost/xxxxxxx/sugar_daddy_member-1.xml i need to retrive the last number in the url. By using this below given code i can sucessfully get the number but if two digit number comes in the url i can retrive only one number.
$page = 'sugar_daddy_member-10';
$last_char = substr($page, 19, 1);
You can use the strrpos function to find the dash.
<?php
$page = 'sugar_daddy_member-10';
$idx = strrpos($page, '-');
$last_char = substr($page, $idx+1);
echo ($last_char);
?>
Output
10
Try this one using PHP explode() Function
<?php
$page = 'sugar_daddy_member-10';
$temp = explode("-",$page);
echo $temp[count($temp)-1];
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't seem to get this correct.
I have this PHP command that I need to do the same thing in Python:
preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
I have three possible string formats:
domain = collabedge-123.dc-01.com
domain = cb123.dc-01.com
domain = ss123.dc-01.com
I need to pull out the 123 and 01 from the string no matter what the format of the string and assign to variables.
You can use this code:
import re
domain = "collabedge-123.dc-01.com"
# preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
regex = r"^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$"
res = re.match(regex,domain)
print(res.group(0))
print(res.group(1))
print(res.group(2))
print(res.group(3))
Output:
collabedge-123.dc-01.com
collabedge-
123
01
As sure as I posted the question I kept trying and figured it out. For those that want to know the answer.
d = re.match(r"(collabedge-|cb|ss)([0-9]+)\.dc-([0-9]+)\.com", domain)
firstnum = d.group(2)
secnum = d.group(3)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
$data[10] = "12.21661,13.00128";
$loc = explode(",",$data[10]);
$lat = $loc[0];
$long = $loc[1]; **// Undefined offset:1**
When i run above code I got error on $long = $loc[1]; line (Undefined offset:1).
How can I resolve this??
The data in string is 2 digits and 5 decimals. if the number have fixed limits then there is function in PHP : str_split()
Here's documentation , Look at an example , it will be much clear.
it will split in exact length like
Check example : Example #1 Example uses of str_split() in documentation
http://php.net/manual/en/function.str-split.php
or
there is explode() function
http://php.net/manual/en/function.explode.php
does exactly what you want , I guess.
check out , examples are quite simple.
Any query comment back.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to get a substring (see examples, bold part) from the string. All strings begin with "input" followed by 2 underscores with some (1 to 7) random chars between. Thank you!
Examples:
input_7ax8_SOME_INFO
input_3f0max2_SOME_OTHER_INFO
input_k_ANOTHERINFO-any-chars-possible:0123456789
Using the detection of "non underscore" + "underscore" times 2 and fetching everything that comes after that you can get the result you ask.
The ?: is meant for not returning the result of the parts with underscores because the () are needed to combine it together.
$input = 'input_k_ANOTHERINFO-any-chars-possible:0123456789';
preg_match( '~^(?:[^_]+_){2}(.*)$~', $input, $match );
var_export($match);
You just need explode and its third param :
<?php
$input = 'input_7ax8_SOME_INFO';
$input = explode("_",$input,2); // Split 2 times
$input[2] = '<b>'.$input[2].'</b>'; // Make the rest of the string bold
$input = implode("_",$input); // re joining
echo $input;
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some problem.
I have some data from my post on date range picker
there are
"10/04/2013 - 10/26/2013"
I want to get
date1 = "10/04/2013"
and
date2 = "10/26/2013"
for my between date query..
please help me
Thank you for your attention
You need to use explode()
$string="10/04/2013 - 10/26/2013"; //Your string
$exploded = explode('-', $string); //Explode using -
echo $exploded[0]; //echo string 1
echo $exploded[1]; //echo string 2
Note: You will have to use trim() if you want to get rid of the white space, else in the explode() first parameter, use the spaces before and after the - like explode(' - ', $string)
For mysql query the date format should be as "YYYY-mm-dd"
Your input comes as MM/dd/YYYY
You may need to convert that to mysql format before firing the query
In that case you can use strtotime()
date("Y-m-d",strtotime("10/04/2013"));
will give you 2013-10-04