1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

preg_replace: How to remove it

Discussion in 'PHP' started by Tuhin1234, Feb 9, 2016.

  1. #1
    Please help me how to use it
    http://www.phpliveregex.com/p/eyZ


     <form method="GET">
            <div class="row">
                <div class="input-group">
                <input name="url" type="text" class="form-control" placeholder="Example http://www.site.com/id124536/05/how_to_make_a_php_site" required>
                <span class="input-group-btn">
                    <button class="btn btn-info" type="submit">DOWNLOAD</button>
                        </span>
            </div><!-- /input-group -->
            </div><!-- /.row -->
        </form>
    <br>
    
    <?php
    function Videos($url)
    {
        $url = str_replace(' ', '', $url);
        if(!empty($url)):
            curl_setopt($curl = curl_init($url), CURLOPT_RETURNTRANSFER, 1);
            preg_match("/flv_url=(.+)url_bigthumb=([^&]+)/i", curl_exec($curl), $infos);
            curl_close($curl);
            return "<a href='".urldecode($infos[1])."'><img class='img-thumbnail' src='".urldecode($infos[2])."' width='300' height='200' border=0 alt='Download'></a>";
        endif;
    }
    
    echo Videos($_GET['url']);
    ?>
          </div>
    Code (markup):
    http://www. site. com/id124536/05/how_to_make_a_php_site
    how to remove when data input
     
    Tuhin1234, Feb 9, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Some context would be good.
    Why do you need to remove that bit? Will that bit always be in the same place? Do you have any control of the rest of the url (how it's formatted, which domain it is, what ID, and so on?)
     
    PoPSiCLe, Feb 9, 2016 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Yeah, I'm really not getting the whole picture here. The form you included seems to have Jack-all to do with the PHP following it and is riddled with mistakes (like where's your fieldset and label, why the button tag to do a input's job, what's with the extra DIV for nothing...) and it's extremely unclear what your CURL is returning or how it relates to that URI you posted after the code.
     
    deathshadow, Feb 9, 2016 IP
  4. Tuhin1234

    Tuhin1234 Active Member

    Messages:
    178
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    Please sir see PM
     
    Last edited: Feb 9, 2016
    Tuhin1234, Feb 9, 2016 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Ok, seeing the subject matter thank you for not publicly putting that in the open area.

    As to your problem, you're looking at this the wrong way.The URI you are having problems with are in fact a 301 redirect. So instead of returning the content you want, it's returning a new "Location" header. By default cURL will not try to follow redirects. You can TRY adding:
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

    Between your first curl_setopt and when you run the curl_exec, but that will not work if PHP's incorrectly named "safe_mode" is active (which there's no reason to have that on any time after 2003) OR if an open_basedir is set (which there are legitimate reasons to use).

    Generally speaking it's sloppy/bad practice to blindly trust that a cURL operation is going to return a "200 ok" response, which is why blindly dumping the result of that exec into a function is just asking for it to break. The method I like to use is to add a loop trapping 300 series for redirects, and bombing if any other non-200 response is passed. Error handling is VERY important, more so if you're going to be extracting direct DL links from a website's flashvars.

    That goes something like this:

    <snip>use code in next post</snip>

    That's untested but should give you an idea of the depth of testing needed to handle all those possible error conditions properly. If I have time later when I'm back at the workdesk I'll take a stab at seeing about dotting the t's and crossing the i's on that... or, uhm, whatever.

    Really trying to parse out the value instead of following the redirect? That's just asking for it to break again moving forward.
     
    Last edited: Feb 9, 2016
    deathshadow, Feb 9, 2016 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Ok yeah, a few typo's... one uneccessary (), two $ before function names (doh), and one double quote that should have been a single. My bad, what I get for writing code in the post box here on the forums on the laptop. Get to the workstation and it's like "well DUH!"

    This should work:

    /* 
    	NOTE redirect_url is only valid in PHP 5.3.7/newer, for older PHP
    	support you'd need to parse the content of the response from cURL
    	
    	Though really at that point, just upgrade to a PHP from THIS century!
    */
    
    function getUri($uri) {
    
    	do {
    	
    		$curl = curl_init($uri);
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    		// set it to TRY and handle it automagically
    		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    		
    		// break the erorr condition handling into steps
    		
    		if (curl_errno($curl)) {
    			$result = [ 'curlError' => $curl_error($curl) ];
    			curl_close($curl);
    			return $result;
    		}
    		
    		$result = [ 'data' => curl_exec($curl) ];
    		if (curl_errno($curl)) {
    			$result['curlError'] = $curl_error($curl);
    			curl_close($curl);
    			return $result;
    		}
    		
    		$result['curlError'] = false;
    		
    		$result['info'] = curl_getinfo($curl);
    		// use switch so if we want to add more handlers it's simpler
    		switch ($result['info']['http_code']) {
    			case 301:
    			case 302:
    				$uri = $result['info']['redirect_url'];
    				break;
    			default:
    				curl_close($curl);
    				return $result;
    		}
    		
    	} while (true);
    	
    } // function getUri
    
    
    function videoFromUrl($url) {
    
    	if (!isset($url) || empty($url = str_replace(' ', '', $url)))
    		return '
    			<div class="error">
    				No File Specified
    			</div>';
    			
    	$result = getUri($url);
    	
    	if ($result['curlError']) return '
    			<div class="error">
    				Error "' . $result['curlError'] . '"
    			</div>';
    	
    	if ($result['info']['http_code'] == 200) {
    		preg_match(
    			"/flv_url=(.+)url_bigthumb=([^&]+)/i",
    			$result['data'],
    			$infos
    		);
    		return '
    			<a href="' . htmlspecialchars(urldecode($infos[1])) . '">
    				<img
    					src="' . htmlspecialchars(urldecode($infos[2])) . '"
    					class="img-thumbnail"
    					width="300" height="200" 
    					alt="Download"
    				>
    			</a>';
    	}
    	
    	return '
    			<div class="error">
    				Unable to recover file. HTTP error code ' .
    				$result['info']['http_code'] . '
    			</div>';
    			
    } // function videoFromUrl
    Code (markup):
    Just call "videoFromUrl" where you were using your "Videos" function. Will give you much more robust error handling and should automatically deal with that redirect... whcih is good. Trying to parse someone else's URL that's redirecting yourself could bite you later if they decide to change the redirect AGAIN. You're better off just following how HTTP header responses are supposed to work in the first place.

    Worked fine here calling the URL you PM'd with the extra directory in it.
     
    deathshadow, Feb 9, 2016 IP