Question

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

Was it helpful?

Solution

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
        )

)
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top