2

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

1 Answer 1

5

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:

<?php 

$file = 'test.txt';

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

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

$matches = array();

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

print_r($matches);

?>

Where the file I reference looks like this:

http://wordpress.stackexchange.com/questions/12809/retrieving-all-links-from-a-post

http://www.youtube.com/

http://ca3.php.net/manual/en/function.preg-match.php

and the return looks like this:

Array
(
    [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
        )

)
3
  • 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 that you have read and understand our privacy policy and code of conduct.

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