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})
Related
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 am trying to parse a badly formed html table:
A couple of lines of this are:
Food:</b> Yes<b><br>
Pool: </b>Beach<b></b><b><br>
Centre:</b> Yes<b><br>
After spending a lot of time on this with Xpath, I think it is probably better to split the above text into lines use preg_split and parse from there.
The pattern I think would work uses:
<\b><\br>*: <\b>
my code is as follows:
$pattern='</b></br>*:</b>';
$pattern=preg_quote($pattern,'#');
$chars = preg_split($pattern, $output);
print_r($chars);
I am getting the following error:
Delimiter must not be alphanumeric or backslash
What I am doing wrong?
Try this:
$pattern='</b></br>*:</b>';
$pattern=preg_quote($pattern,'#');
$chars = preg_split('#'.$pattern.'#', $output);
print_r($chars);
The preg_quote function just makes it safely escaped, it doesn't actually add the delimiters for you.
As other people will surely point out, using regular expressions is not a good way to parse HTML :)
Your regular expression is also not going to match what you hope. Here's a version that will probably work for your input:
$in = " Pool: </b>Beach<b></b><b><br>";
$out = explode(':', strip_tags($in));
$key = trim($out[0]);
$value = trim($out[1]);
echo "$key = $value\n";
This removes all the HTML, then splits on the colon, and then removes any surrounding whitespace.
Your pattern needs to start and end with a delimiter; looks like you're using # if I'm reading this correctly, so you should have $pattern = '#</b></br>.*:</b>#';.
Also, you're mixing things up; * is not a simple wildcard in regex. If you mean "any number of any characters," the pattern you need is .*. I've included this above.
so there's a string,
<?php
$string = <<<STR
/\!##$%^&*()?.,djasijdiwqpk,=-c./zcxzo123154897kp02ldz.,world90iops02&&&8ks
STR;
I want to replace everything to NULL, except word "world" and number 1 and 3,
I just want to get "world13" or "world31" from that string USING regular expressions
I have already implemented basic solution,
via strpos() and substr() and this is works as excepted. But I need to do this via RegExp
The question is:
Is it possible to extract that word using RegEx?
~(world(?:(31|13))~i. The 'i' makes the regex case insensitive. The ?: is there so it doesn't put it in the matches array in a separate result. Wouldn't say it's very complex, by the way :) If you want every 1 and 3 in there, you can use ~(world|1|3)~i.
Is it possible to extract that word using RegEx?
Yes. You can use this regular expression:
(world)
I know, that, But I can't extract world13 or world31
Ah, I understand! You can use:
$string = preg_replace('/.*/s', 'world13', $string);
A simple solution is to find things you need and then join them to a string.
preg_match_all('/world|[13]/', $string, $matches);
$ret = join($matches[0]);
Is it possible to have a PHP regex expression that extracts the content from the first ] to the last [?
For example if I had the following string:
$string = [shortcode]You write a shortcode by using ([])[/shortcode]
I would want to extract:
You write a shortcode by using brackets ([])
and store it in a variable. The content to be extracted could be anything. Thanks in advance.
You should be using capturing groups to make sure you match the closing tag.
\[(\w+)\].*?\[/\1\]
This will match a word inside [] and keep going until if finds the same word inside [/...].
Regexes are greedy by default, so this will do the job just fine:
/\](.*)\[/
To get this working in PHP properly, you would do something like this:
preg_match('/\](.*)\[/', $text, $matches);
$result = $matches[1];
this could make, what you need
[^\]]\](.*)\[[^\[]
This works:
preg_match( '#\](.*)\[#', $string, $matches);
print_r($matches);
I'm trying to extract src attributes from: [attname src="http://example.org"] somecontent [attname src="http://www.example.com"]
What I have now:
preg_match_all('#attname src=".*[^"]#', $buffer, $bufferarr);
However it doesn't work - there's no stop after second ", what results in: attname src="http://example.org"] somecontent [attname src="http://www.example.com
By default, + and * are "greedy" - they gobble up as many characters as they can. That's why you get more than you want. If you add ? to them (+? and *?) they will be non-greedy and will stop as soon as they can.
You regexp also looks wrong. It should be something like #attname src="[^"]*?"#.
preg_match_all('#attname src="([^"]*)"#', $buffer, $bufferarr);
Not the best solution but anyways it get's the job done :
$str = '[attname src="http://example.org"] somecontent [attname src="http://www.example.com"]';
preg_match_all('/attname src=\"(.*?)\"/', $str, $match);
var_dump($match);