I'm trying to use PHP regular expressions. I've tried this code:
$regex = "c:(.+),";
$input = "otherStuff094322f98c:THIS,OtherStuffHeree129j12dls";
$match = Array();
preg_match_all($regex, $input, $match);
It should return a sub-string THIS ("c" and ":" followed by any character combination followed by ",") from $input. But it returns a empty array. What am I doing wrong?
I think you need the slashes to make regex working.
and using .+ will match everything behind the comma too, which is you don't want. Use .+? or [^,]+
$regex = "/c:(.+?),/";
or
$regex = "/c:([^,]+),/";
Related
i need to explode youtube url from this line:
[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]
It is possible? I need to delete [embed] & [/embed].
preg_match is what you need.
<?php
$str = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
preg_match("/\[embed\](.*)\[\/embed\]/", $str, $matches);
echo $matches[1]; //https://www.youtube.com/watch?v=L3HQMbQAWRc
$string = '[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]';
$string = str_replace(['[embed]', '[/embed]'], '', $string);
See str_replace
why not use str_replace? :) Quick & Easy
http://php.net/manual/de/function.str-replace.php
Just for good measure, you can also use positive lookbehind's and lookahead's in your regular expressions:
(?<=\[embed\])(.*)(?=\[\/embed\])
You'd use it like this:
$string = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
$pattern = '/(?<=\[embed\])(.*)(?=\[\/embed\])/';
preg_match($pattern, $string, $matches);
echo $match[1];
Here is an explanation of the regex:
(?<=\[embed\]) is a Positive Lookbehind - matches something that follows something else.
(.*) is a Capturing Group - . matches any character (except a newline) with the Quantifier: * which provides matches between zero and unlimited times, as many times as possible. This is what is matched between the groups prior to and after. This are the droids you're looking for.
(?=\[\/embed\]) is a Positive Lookahead - matches things that come before it.
I have these arrays (array and array2)
$urls = array("http://piggington.com/pb_cash_flow_positive")
I have this regular expression
(preg_match("/\/{2}.*?\./", $array[$i], $matches))
It checks for everything that comes after 2nd slash and before 1st dot. So it will find
/piggington.
Now want to concatenate a variable inside the following regular expression, so it will search for a specific string.
I tried:
$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);
But it's not finding any matches.. What am I doing wrong? It should be looking inside array2 and making a positive match with $matches_imploded
between second slash and first dot we found $matches_imploded
To match everything which comes after // and before the first dot, you need to use \K or positive lookbehind.
preg_match("~/{2}\K[^.]*(?=.)~", $array[$i], $matches)
$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);
I'm trying to get regex to work to take everything after "test" and before "#" in an email so "test-12345#example.com would become 12345.
I've got this far to get it to return everything before the "#" symbol. (Working in PHP)
!(\d+)#!
Either you can use capturing groups and use the regex
test-(\d+)#
and use $1 or use lookaheads and behinds like (?<=test-)\d+(?=#) which will just match 12345
(?<=test-)[^#]+
You can try this.No need to use groups.See demo.
https://regex101.com/r/eZ0yP4/28
You want everything between test and # so don't use \d.
$myRegexPattern = '#test([^#])*##Ui';
preg_match ($myRegexPattern, $input, $matches);
$whatYouNeed = $matches[1];
Try this
$input = 'test-12345#example.com';
$regexPattern = '/^test(.*?)\#/';
preg_match ($regexPattern, $input, $matches);
$whatYouNeed = $matches[1];
var_dump($whatYouNeed);
I've searched for an example of this, but can't seem to find it.
I'm looking to replace everything for a string but the #texthere
$Input = this is #cool isn't it?
$Output = #cool
I can remove the #cool using preg_replace("/#(\w+)/", "", $Input); but can't figure out how to do the opposite
You could match #\w+ and then replace the original string. Or, if you need to use preg_replace, you should be able to replace everything with the first capture group:
$output = preg_replace('/.*(#\w+).*/', '\1', $input);
Solution using preg_match (I assume this will perform better):
$matches = array();
preg_match('/#\w+/', $input, $matches);
$output = $matches[0];
Both patterns above do not address the issue how to handle inputs which match multiple times, such as this is #cool and #awesome, right?
Some links like these:
[links url='http://www.google.com.hk' title='Google' image='']google[/links]
[links url='http://hk.yahoo.com' title='yahoo' image='']yahoo[/links]
how to use PHP Regular expression get the url attributes? Thanks.
http://www.google.com.hk
http://hk.yahoo.com
This should get you started:
preg_match_all("/\[links[^\]]+url='([^']+)'/", '{{your data}}', $arr, PREG_PATTERN_ORDER);
Explanation of the regex:
/
\[ //An excaped "[" to make it literal.
links //The work "links"
[^\]]+ //1+ non-closing brackent chars ([^] is a negative character class)
url=' //The work url='
([^']+) //The contents inside the '' in a caputuring group
/
Use this regex: /\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/
$str = "[links url='http://www.google.com.hk' title='Google' image='']google[/links]";
$m = array();
preg_match('/\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/', $str, $m);
echo $m[1];