0

I am using this code to download video from you tube (it is a robot.php file used in downloading process)

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

require_once('lib/youtube.lib.php');
if(eregi('youtube.com|localhost',$_GET['url'])){
    if(!eregi('www.',$_GET['url'])){
        $_GET['url'] = str_replace('http://','http://www.',$_GET['url']);
    }
    list($video_id,$download_link) = get_youtube($_GET['url']);
            ?>
    <p>
    <img src="http://img.youtube.com/vi/<?php echo trim($video_id);?>/1.jpg" alt="Preview 1" class="ythumb" />
    <img src="http://img.youtube.com/vi/<?php echo trim($video_id);?>/2.jpg" alt="Preview 2" class="ythumb" />
    <img src="http://img.youtube.com/vi/<?php echo trim($video_id);?>/3.jpg" alt="Preview 3" class="ythumb" />
    </p>
    <p>
    <a href="<?php echo trim($download_link);?>" class="ydl" title="Download as FLV">Download FLV</a>
    <a href="<?php echo trim($download_link);?>&fmt=18" class="ydl" title="Download as MP4">Download MP4</a>
    <a href="<?php echo trim($download_link);?>&fmt=17" class="ydl" title="Download as 3GP">Download 3GP</a>
    </p>
    <?php
}
else{
    die('<span style="color:red;">Sorry, the URL is not recognized..</span>');
}
?>

running this, I get error

Deprecated: Function eregi() is deprecated in D:\wamp\www\u\code\robot.php on line 6

and line 6 is

if(eregi('youtube.com|localhost',$_GET['url']))

searching stackoverflow i got

if (!function_exists('eregi')) {
        function eregi($find, $str) {
            return stristr($str, $find);
        }
    }

but i am not sure how to use it? where should i place it?

could any one help me regarding this? how to update this code to match regex and remove errors?

thanks..

Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77
  • 1
    possible duplicate of [Converting ereg expressions to preg](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Jan 22 '12 at 22:36

2 Answers2

2

As of PHP 5.3.0, the POSIX Regex extension is deprecated in favour of the PCRE extension.

See http://php.net/reference.pcre.pattern.posix for a short overview on migrating your POSIX regex code (ereg*) to use the PCRE (preg_*) functions, necessary alterations to your regular expressions and limitations of both extensions.


Also, you should not use the code that you found. The PHP error is stating that the function exists but is deprecated. The code that you found will do nothing to help.


An example of a common "conversion" might look like:

POSIX if (eregi('apple|pear')) …

PCRE if (preg_match('/apple|pear/i')) …

What was done?

salathe
  • 51,324
  • 12
  • 104
  • 132
  • thanks, can you explain it a bit more? what should i do? i do want to try this code and want to download youtube video using this code? – Zaffar Saffee Jan 22 '12 at 22:25
  • I was referring to the code that you found on stackoverflow, the part with `function_exists()`. As for explaining it, read the link. The solution is to use alternative functions instead of `eregi()`, such as `preg_match()` and `strpos()`. – salathe Jan 22 '12 at 22:32
  • if(eregi('youtube.com|localhost',$_GET['url'])){ if(!eregi('www.',$_GET['url'])){ $_GET['url'] = str_replace('http://','http://www.',$_GET['url']); } list($video_id,$download_link) = get_youtube($_GET['url']); can you convert this code to regex please? – Zaffar Saffee Jan 22 '12 at 22:36
  • Please utilize the resources mentioned above and you will be able to do it yourself easily. – salathe Jan 22 '12 at 22:44
  • thanks for the guideline ...
    [link]http://stackoverflow.com/questions/8977169/youtube-video-download-in-php-using-regex please visit this one too
    – Zaffar Saffee Jan 24 '12 at 18:34
1

As mentioned by salathe, ereg functions are deprecated now, hence the deprecated notice. Use preg_match function instead (you could also hide showing notices, but this won't solve the cause, it will just hide it). The preg_match function has a little bit bit different syntax than eregi.

Replace line

if(eregi('youtube.com|localhost',$_GET['url'])){

with

if(preg_match('/youtube\.com|localhost/i',$_GET['url'])){

Note the i at the end of the pattern indicating the match should be case insensitive. You also have to escape the ., since this is special regexp character.

Ondrej Machulda
  • 998
  • 1
  • 12
  • 24