Skip to content

Instantly share code, notes, and snippets.

@Jany-M Jany-M/php_regex.php
Last active Oct 10, 2018

Embed
What would you like to do?
[PHP] Common RegEx
<?php
// Let's be honest, RegEx is not fun, so these may come in handy
// I'll update this in case I write/find more
$text = '[video mp4="http://somevideo.com/abc"]';
preg_match_all("\bmp4="(.+)\b", $text, $matches);
var_dump($matches[0]);
// http://somevideo.com/abc
$text = '[gravityform description="true" id="23" title="true"]';
preg_match_all("\bid="([0-9]+)\b", $text, $matches);
var_dump($matches[0]);
// 23
// Get Youtube ID from url
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video_url, $match);
var_dump($match[1]);
// XZ-Hr7Onr7k
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.