i'm using the default "insert video" function from wordpress (which inserts a normal hyperlink to the video only) and would like to replace that link with something else like a video player.

my question: what's the regex pattern for grabbing all links inside a post? thx


1 Answer 1


I'll answer your question below, but have you looked at using embeds? Look here for more information: http://codex.wordpress.org/Embeds

The simplest regex for this would look something like http\:\/\/.*\b

Here's an example of it in action:


$file = 'test.txt';

$fp = fopen($file, 'r');

$contents = fread($fp, filesize($file));

$matches = array();

preg_match_all('/http\:\/\/.*\b/', $contents, $matches);



Where the file I reference looks like this:




and the return looks like this:

    [0] => Array
            [0] => http://wordpress.stackexchange.com/questions/12809/retrieving-all-links-from-a-post
            [1] => http://www.youtube.com
            [2] => http://ca3.php.net/manual/en/function.preg-match.php

  • I'm not sure why my code is red here. It syntax highlighted correctly in the edit view. Mar 23, 2011 at 12:38
  • @Lea Cohen: Thanks! Sadly, that didn't solve it :( Mar 23, 2011 at 12:59
  • 2
    The [regex] tag for the question forced regex-style syntax highlighting on the answers too, instead of the default style which works for PHP. You can now give a language hint, and I edited your question to do it (partly because this was the first time I could try this, and I was curious).
    – Jan Fabry
    Mar 23, 2011 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.