3

I would like to check in PHP, if a string contains an URL to a specific domain and save it in a new string.

example: "Check out my latest video here http://www.youtube.com/?123"

So now the new string should have the value "http://www.youtube.com/?123".

If there is another link in the example, but not from youtube, the string should still only contain the youtube value.

user1264272
  • 43
  • 1
  • 4
  • Exactly how do you determine what youtube url you want in the new string if no youtube url was found in the original? – Marc B Mar 12 '12 at 14:15
  • 1
    possible duplicate of [extract urls from text in PHP](http://stackoverflow.com/questions/910912/extract-urls-from-text-in-php) – Marc B Mar 12 '12 at 14:15
  • You have this one answered already. Check it out here: http://stackoverflow.com/questions/3392993/php-regex-to-get-youtube-video-id – dexter.ba Mar 12 '12 at 14:17

4 Answers4

2

Here is example function.

function IsYoutubeUrl($url)
{
    return preg_match("#^https?://(?:www\.)?youtube.com#", $url);
}

This function should work with HTTP and HTTPS, along with or without www (it doesn't matter) how it will be written.

If you do not expect the URL in the beginning of the string $url you could remove ^.

Rolice
  • 3,063
  • 2
  • 24
  • 32
  • i get this error message: Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /home/.sites/123/site602/web/feeds/index.php on line 5. will this also work if the link looks like this here – user1264272 Mar 12 '12 at 14:42
  • My mistake @user1264272, now it should be working, I use as delimiter **#**. – Rolice Mar 12 '12 at 14:44
0

This is my own function to replace a youtube link on a string with the video embeded:

(youtube video on wordpress format: [youtube=http://www.youtube.com/watch?v=zuqoGJ4Y5NM])

                function youtube($volcado) {                        
                    if( ereg('\[youtube=',$volcado) ) {
                       //YOUTUBE CODE   
                       $posicion=stripos($volcado,"[youtube=");
                       $volcado1=substr($volcado,0,$posicion);
                       $posicion=$posicion+9;
                       $volcadotemp=substr($volcado,$posicion,strlen($volcado)-$posicion);
                       $posicion2=strpos($volcadotemp,"]");
                       $enlace_video=substr($volcadotemp,0,$posicion2);
                       $volcado2=substr($volcadotemp,$posicion2+1,strlen($volcadotemp)-$posicion2-1);
                       $posvideo=stripos($enlace_video,"watch?v=");
                       $codigo_video=substr($enlace_video,$posvideo+8,strlen($enlace_video)-1);
                       $enlace_video="http://www.youtube.com/v/".$codigo_video;
                       $codigo_video="<br><object width=\"425\" height=\"350\"><param name=\"movie\" value=\"".$enlace_video."\"></param><embed src=\"".$enlace_video."\" type=\"application/x-shockwave-flash\" width=\"425\" height=\"350\"></embed></object><br>";
                       return $volcado1.$codigo_video.youtube($volcado2);
                       //FIN DE YOUTUBE CODE
                    } else {
                       return $volcado;                     
                    }                       
                }
joan16v
  • 5,055
  • 4
  • 49
  • 49
0

PHP has an inbuilt function : fnmatch

fnmatch('* http://www.youtube.com/* *', string) should probably work.
damned
  • 935
  • 2
  • 19
  • 35
0

Your specific domain in the following example is "youtube.com"

$sText =  "Check out my latest video here http://www.youtube.com/?123";
preg_match_all('@https?://(www\.)?youtube.com/.[^\s.,"\']+@i', $sText, $aMatches);
var_dump($aMatches);
Leif
  • 2,143
  • 2
  • 15
  • 26
  • this seems not to work when the link looks like this: here – user1264272 Mar 12 '12 at 14:45
  • No problem because that is not a valid link to youtube.com. It needs "http://" in front, otherwise it is interpreted like an internal link of the current domain. Try it out in your browser. – Leif Mar 12 '12 at 14:51
  • hi, i got it working, but only when there is a "www" in the link. i just realized another issue. sometimes, there are more then one youtube links in the string, but preg_match only looks for the first one. how can i code that? – user1264272 Mar 12 '12 at 17:43
  • The thing with www: fixed. I forgot the "?". Use preg_match_all() to get all matches. – Leif Mar 12 '12 at 19:25
  • thanks i figured it all out yesterday with a little trial and error. – user1264272 Mar 13 '12 at 08:59