I have the following date like this
20120809
I want to separate this with '-' and make this to
2012-08-09
using regex.
Is there is a simple solution to this using PHP? Anything will be fine.
You could just use date_parse_from_format, the docs are here. In your case, that would make for:
date_parse_from_format('Ymd',$dateIn);
Gives an array back, so you can pretty much do as you please from there on end
As #Rocket said, you can also get the DateTime object, which gives you all sorts of goodies, too. more docs to read ;)
use date() and strtotime()?
date('Y-m-d',strtotime('20120809'));
Agreed with others that you don't need a regex to do this, but...
preg_replace('/(\d{4})(\d\d)(\d\d)/', '\1-\2-\3', $str);
With PHP though, you can use substr_replace to insert dashes:
$str = substr_replace($str, '-', 4, 0);
$str = substr_replace($str, '-', 7, 0);
If you want to use a regex, it's simple. Just use \d with a quantifier like {4}.
preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', '20120809');
Using regex for this thing is completely wrong and there are better ways to do that like substr() ...
But here is what you want:
<?php
$string = '20120809';
preg_match( '/(\d{4})(\d{2})(\d{2})/', $string, $parts);
unset($parts[0]);
$result = implode('-', $parts); //2012-08-09
?>
Related
I got a simple php issue which is that I don't know how to use substring here...
ABCDEF[RAND NUMBER 1-10000000]GH
And so I need to get that random number using substring,
I dont know if my brain works correctly today but I really couldnt figure how to do that.
Regex is always better in this case but If your string pattern is fixed like ABCDEF123456GH, you can simply use substr like
$str = "ABCDEF432465GH";
echo substr($str, 6, -2);
This will do, no need for substr or anything:
$string='ABCDEF432465GH';
echo preg_replace('/[a-z\[\] ]*/i', '', $string);
https://eval.in/136918
Regular expressions are better when you don't know where in the text is desired substring,
$string = "ABCDEF10000000GH";
if (preg_match("/(\d+)/", $string, $m)) {
print $m[1];
}
Hey I'm filtering a string and want it to go from:
512MBGDDR5videogeheugen
To:
512MB
So I tried php preg replace and did this:
$filterString = preg_replace("/[^0-9]+(KB|MB|GB)/", "", $string);
Does anyone know a way to solve this?
THANKS FOR THE RESPONSE!
Instead of replacing you can get your match like this.
preg_match("/^([0-9]+(KB|MB|GB))/", $string, $results);
$filterString = $results[0];
You can also use T-Regx library that has automatic delimiters:
pattern('^[0-9]+(KB|MB|GB)')->match($string)->all();
I'm actually not home so I tried a javascript regex, but I think it should work:
$filtered = preg_replace('^([0-9]+(KB|MB|GB))(.+)$','$1',$string)
So I've seen a couple articles that go a little too deep, so I'm not sure what to remove from the regex statements they make.
I've basically got this
foo:bar all the way to anotherfoo:bar;seg98y34g.?sdebvw h segvu (anything goes really)
I need a PHP regex to remove EVERYTHING after the colon. the first part can be any length (but it never contains a colon. so in both cases above I'd end up with
foo and anotherfoo
after doing something like this horrendous example of psuedo-code
$string = 'foo:bar';
$newstring = regex_to_remove_everything_after_":"($string);
EDIT
after posting this, would an explode() work reliably enough? Something like
$pieces = explode(':', 'foo:bar')
$newstring = $pieces[0];
explode would do what you're asking for, but you can make it one step by using current.
$beforeColon = current(explode(':', $string));
I would not use a regex here (that involves some work behind the scenes for a relatively simple action), nor would I use strpos with substr (as that would, effectively, be traversing the string twice). Most importantly, this provides the person who reads the code with an immediate, "Ah, yes, that is what the author is trying to do!" instead of, "Wait, what is happening again?"
The only exception to that is if you happen to know that the string is excessively long: I would not explode a 1 Gb file. Instead:
$beforeColon = substr($string, 0, strpos($string,':'));
I also feel substr isn't quite as easy to read: in current(explode you can see the delimiter immediately with no extra function calls and there is only one incident of the variable (which makes it less prone to human errors). Basically I read current(explode as "I am taking the first incident of anything prior to this string" as opposed to substr, which is "I am getting a substring starting at the 0 position and continuing until this string."
Your explode solution does the trick. If you really want to use regexes for some reason, you could simply do this:
$newstring = preg_replace("/(.*?):(.*)/", "$1", $string);
A bit more succinct than other examples:
current(explode(':', $string));
You can use RegEx that m.buettner wrote, but his example returns everything BEFORE ':', if you want everything after ':' just use $2 instead of $1:
$newstring = preg_replace("/(.*?):(.*)/", "$2", $string);
You could use something like the following. demo: http://codepad.org/bUXKN4el
<?php
$s = 'anotherfoo:bar;seg98y34g.?sdebvw h segvu';
$result = array_shift(explode(':', $s));
echo $result;
?>
Why do you want to use a regex?
list($beforeColon) = explode(':', $string);
I'm trying to replace 0-1. with 0-1.<br> how do i do that?
update:
Sorry for my vague question. You guys misunderstood me. '0-1.' is the pattern i want to replace, which means the pattern should be like `"/(\d)+(-)*(\d)*\./"` and the string may be '1.' '0-1.' or something that expression could represent
You can use a standard PHP function:
str_replace('0-1.', '0-1.<br>', $yourString);
How about:
preg_replace("/(\d+(?:-\d+)?\.)/", "$1<br>", $string);
You can use preg_replace like this:
preg_replace("/(0-1\.)/", "$1<br>", $string);
or, as you know the substitution already:
preg_replace("/0-1\./", "0-1.<br>", $string);
There has always been a confusion with preg_match in php.
I have a string like this:
apsd_01_03s_somedescription
apsd_02_04_somedescription
Can I use preg_match to strip off anything from 3rd underscore including the 3rd underscore.
thanks.
Try this:
preg_replace('/^([^_]*_[^_]*_[^_]*).*/', '$1', $str)
This will take only the first three sequences that are separated by _. So everything from the third _ on will be removed.
if you want to strip the "_somedescription" part: preg_replace('/([^]*)([^]*)([^]*)(.*)/', '$1_$2_$3', $str);
I agree with Gumbo's answer, however, instead of using regular expressions, you can use PHP's array functions:
$s = "apsd_01_03s_somedescription";
$parts = explode("_", $s);
echo implode("_", array_slice($parts, 0, 3));
// apsd_01_03s
This method appears to execute similarly in speed, compared to a regular expression solution.
If the third underscore is the last one, you can do this:
preg_replace('/^(.+)_.+?)$/', $1, $str);