Php template parsing instance

Source: Internet
Author: User
The following is an example of php template parsing. Use a regular expression to Convert {youkuid_XMTc5ODgwOTA4} to the corresponding playback code. & Lt ;? Phpheader (& amp; #39; Content-type: text/html; charsetutf-8 & amp; #39;); $ template & amp; #39; {youkuid


The following is an example of php template parsing.

Use a regular expression
{Youku id_XMTc5ODgwOTA4} refers to the corresponding playback code.

 


Header ('content-type: text/html; charset = utf-8 ');

$ Template ='

{Youku id_XMTc5ODgwOTA4}

{Youku id_XMzA5NTUwOTky}

{Youku id_XMzEwMTQyMjA4}

{Youku id_XMjYzNTE0MTA4}

{Youku id_XMzEwNDMwODk2}

{Youku id_XMjk4NjA3Njk2}

{Youku id_XMjQ3ODQwNDYw}

{Youku id_XMjk4MjA2NDI0}

{Youku id_XMzEwNDIyMTY4}

{Youku id_XMzA5ODUzMjI4}

{Youku id_XMzEwMzMxNjc2}

{Youku id_XMzEwMTk4Njky}

{Youku id_XMTA3OTA4MzQw}

{Youku id_XMjQ1NzI1ODcy}

{Youku id_XMjI4Mzc0OTY0}

';

 

$ Export youku = array ("rege" => "/\ s * \ {youku \ s * id _(. *)} \ s */I "," callback "=>" compile "));

 

Foreach ($ export Youku as $ val ){

If (isset ($ val ["callback"])

{

$ Template = preg_replace_callback ($ val ["rege"], $ val ["callback"], $ template );

}

}

 

 

/**

*

* Forjobs

* @ Param unknown_type $ matched

*/

Function compile ($ matched)

{

$ Url = $ matched [0]; www.2cto.com

Preg_match ("/id \ _ (\ w +)/", $ url, $ matches );

If (empty ($ matches )){

Preg_match ("# v_playlist \/#", $ url, $ mat );

If (! $ Mat) return false;

 

$ Html = self: _ fget ($ url );

Preg_match ("# videoId2 \ s * = \ s * \ '(\ w +) \' #", $ html, $ matches );

If (! $ Matches) return false;

}

$ Link = "links? Password = & ran = 2513 & n = 3 ";

$ Retval = fileGetContent ($ link );

If ($ retval ){

$ Json = json_decode ($ retval, true );

$ Data ['IMG '] = $ json ['data'] [0] ['logo'];

$ Data ['title'] = $ json ['data'] [0] ['title'];

$ Data ['URL'] = $ url;

$ Data ['swf '] = "http://player.youku.com/player.php/sid/?#matches=1##/v.swf ";

If ($ data) $ data ['object'] = "". $ data ['title']. ""."";

Return $ data ['object'];

} Else {

Return false;

}

}

 

/**

*

* @ Author murain

* @ Param String $ url

*/

Function fileGetContent ($ url)

{

$ Ch = curl_init ();

$ Timeout = 10;

Curl_setopt ($ ch, CURLOPT_URL, $ url );

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );

$ Contents = trim (curl_exec ($ ch ));

Curl_close ($ ch );

Return $ contents;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.