Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Why not just add this into the other question? wordpress.stackexchange.com/questions/12806/… –  t31os Mar 23 '11 at 11:46
add comment

1 Answer 1

up vote 5 down vote accepted

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
        )

)
share|improve this answer
    
I'm not sure why my code is red here. It syntax highlighted correctly in the edit view. –  Gavin Anderegg Mar 23 '11 at 12:38
    
@Lea Cohen: Thanks! Sadly, that didn't solve it :( –  Gavin Anderegg Mar 23 '11 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 '11 at 13:22
    
@Jan Fabry: Awesome! Thank you! –  Gavin Anderegg Mar 23 '11 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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