I have a string Like this:
MyText (1,151)
I would like to get with regex only the value inside (), in this case only: 1,151.
I know it is simple but I am not good with regex.
Thanks!
A nearly identical question was just asked. There are solutions in there that will work for you ;)
This will work:
if (preg_match($str, '/\(([^)]*)\)/', $matches))
{
$content = $matches[1];
}
Related
I need help with a PHP regular expression that would match the sample shortcode below:
[smiley set_name="Happy" filename="smiling.gif"]
I'd like to extract "Happy" and "smiling.gif" from the above shortcode. I would highly appreciate your help. Thanks in advance.
I'm not sure how much of your string you can assume to know but maybe something like:
/\[smiley set_name="([\w\s]+)" filename="(\w+.gif|jpg|png|jpeg)"\]/
So your code may look like:
$string = '[smiley set_name="Happy" filename="smiling.gif"]';
preg_match('/\[smiley set_name="([\w\s]+)" filename="(\w+.gif|jpg|png|jpeg)"\]/', $string, $matches);
var_dump($matches);
I hope somebody could help me out with some „preg_replace“ skills.
I have the following URL:
http://www.domain.com/goto/test-string/
Now I just want to get the „test-string“ part of the URL.
Any idea how I can solve this with preg_replace?
Thanks in advance already!!!
Best,
Florian
I'd suggest using parse_url() to get the pieces:
http://php.net/manual/en/function.parse-url.php
Then using explode( '/', $sUrl ); to get the string as needed.
http://php.net/manual/en/function.explode.php
For dynamic parsing you may need to tweak some more.
It seems like preg_match() would be a better tool for you:
if (preg_match('/([^/]*)/$', $url, $m) {
echo "The string you are looking for is $m[1]<br />\n";
}
but since you specifically asked for preg_replace() I suppose you could do something like this:
$foo = preg_replace('/^.*?/([^/]*)/$/', '$1', $url);
EDIT: I had forgotten to escape my slashes within the regex. Easier to just replace the regex delineators with pipe-symbols so that slashes are no longer special.
$foo = preg_replace('|^.*?/([^/]*)/$|', '$1', $url);
Please Try this one..
$url = 'http://www.domain.com/goto/test-string/hi/';
preg_match('/.*\/(.*?)\//',$url,$match);
echo $match[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)
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);
I am learning regex. I have a very simple question:
I have a long string of content in php.I would like to convert all places where it says:
http://www.example.com/en/rest-of-url
to
http://en.example.com/rest-of-url
Could somebody help me with this? I think I use preg_replace for this?
Bonus: If you have a link to a good site which explains how to do the simplest things like this in regex, please post it. Every regex resource I look at gets very complicated very fast (even the Wikipedia article).
In PHP:
$search = '~http://www.example.com/([^/]+)/(.+)~';
$replace = 'http://$1.example.com/$2';
$new = preg_replace( $search, $replace, $original );
http://regexlib.com/
has a nice regular expression cheat sheet and tester
Assuming:
preg_replace($regex, $replaceWith, $subject);
$subject is the original text. $regex should be:
'#http://([^\.]*)\.example\.com/en/(.*)#'
$replaceWith should be:
'http://$1.example.com/$2'
EDITED: In my orignial answer, I had missed the fact that you wanted to capture part of the domain name.
This will work with any domainname:
$url = 'http://www.example.com/en/rest-of-url';
echo preg_replace('%www(\..*?/)(\w+)/%', '\2\1', $url);
gives:
http://en.example.com/rest-of-url
Reference: preg_replace
You can learn about basic regex, however for your simple question, there's no need for regex.
$str="http://www.example.com/en/rest-of-url";
$s = explode("/",$str);
unset( $s[3]);
print_r( implode("/",$s) ) ;
This is a great site for Regex Tutorials
http://www.regular-expressions.info/
Regex Tutor