PHP Preg match numbers with a letter combination after it? - php

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)

Related

Find and replace "[" and,or "]" using regular expression in PHP

I'm searching domains from emails inside texts, email's "format" is like: [mailto:name#domain.com] and I'm finding them using this basic regular expression:
$r = '/mailto:.*\]/';
then I'm aplying this:
substr(strrchr($matches[1][0], "#"), 1);
and final result is something like
domain.com]
So the question is, how to get rid of "]" or a better way to get only a domain from an email inside [mailto:name#domain.com] any sugestion? Thanks in advance!
Thanks!
You can try lookahead in the regex:
$r = '/mailto:.*(?=\])/';
or just remove it from the result using trim:
$final = substr(strrchr($matches[1][0], "#"),1).trim("]");
And btw, you can just use lookbehind, so you don't need to use the substr:
$r = '/(?<=\[mailto:[^#]*#).*(?=\])/';
Change your expression to this.
$r = '/mailto:[^#]+#[^]]+/';
You can do this without using substr and a basic regular expression.
preg_match_all('/\[mailto:[^#]+#([^#]+)\]/', $str, $matches);
print_r($matches[1]);
See working demo
Use rtrim
'domain.com]'.rtrim("]");
Or you can just try to extract the possible elements from the string:
([\\w-+]+(?:\\.[\\w-+]+)*#(?:[\\w-]+\\.)+[a-zA-Z]{2,7})

preg_match dose not work correctly in php

all,
I am using preg_match to filter some data, and it is strange that, it dose not work correctly. I am new to regex, and I used a php live regex website to check my regex, which works correctly. So I have no idea what is wrong here.
I would like to have preg_match to find something like "a\_b" in the $string:
$string="aaa\_bbb:ccc"
if(preg_match("/[a-zA-Z]\\_[a-zA-Z]/", $string)){
$snew = str_replace('\_', "_", $string);
}
But it is strange that even I have a $string like in this example above, the result of preg_match is 0. But when I change it to
preg_match("/\\_[a-zA-Z]/", $string)
It works fine and return 1. But of course that is not what I want. Any idea?
Thanks very much~
You don't really need the preg_match at all, from what I can see.
However the problem you're having with it is to do with escaping.
You have this: "/[a-zA-Z]\\_[a-zA-Z]/"
You've correctly identified that the backslash needs to be escaped, however, you've missed a subtle issue:
Regular expressions in PHP are strings. This means that you need to escape it as a string as well as a regular expression. In effect, this means that to correctly escape a backslash so it is matched as an actual backslash character in your pattern, you actually need to have four backslashes.
"/[a-zA-Z]\\\\_[a-zA-Z]/"
It's not pretty, but that's how it is.
Hope that helps.
use:
if(preg_match("/[a-zA-Z]\\\\_[a-zA-Z]/", $string))
instead
You don't need the preg_match altogether, instead just do a replace using this regex:
/([a-zA-Z])\\\\_([a-zA-Z])/
and then replace with $1_$2, like this:
$result = preg_replace("/([a-zA-Z])\\\\_([a-zA-Z])/", "$1_$2$, $string);

Regular expression Date separation

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
?>

How to use regex to append string to the matched results

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);

preg_replace on the matches of another preg_replace

I have a feeling that I might be missing something very basic. Anyways heres the scenario:
I'm using preg_replace to convert ===inputA===inputB=== to inputA
This is what I'm using
$new = preg_replace('/===(.*?)===(.*?)===/', '$1', $old);
Its working fine alright, but I also need to further restrict inputB so its like this
preg_replace('/[^\w]/', '', every Link or inputB);
So basically, in the first code, where you see $2 over there I need to perform operations on that $2 so that it only contains \w as you can see in the second code. So the final result should be like this:
Convert ===The link===link's page=== to The link
I have no idea how to do this, what should I do?
Although there already is an accepted answer: this is what the /e modifier or preg_replace_callback() are for:
echo preg_replace(
'/===(.*?)===(.*?)===/e',
'"$1"',
'===inputA===in^^putB===');
//Output: inputA
Or:
function _my_url_func($vars){
return ''.$vars[2].'';
}
echo preg_replace_callback(
'/===(.*?)===(.*?)===/',
'_my_url_func',
'===inputA===inputB===');
//Output: inputB
Try preg_match on the first one to get the 2 matches into variables, and then use preg_replace() on the one you want further checks on?
Why don't you do extract the matches from the first regex (preg_match) and treat thoses results and then put them back in a HTML form ?

Categories