Redbox
Redbox

Reputation: 1477

PHP: Preg_match (new challenge)

I have a html look like this (its javascript for flash player):

var flashvars =
            {
                'autoplay'          : autoplay,
                'autoreplay'        : 'false',
                'start'             : '',
                'hideLinkPane'      : 'true',
                'disable_sharebar'  : 'true',
                'disablePauseroll'  : disablePauseroll,
                'video_url'         : encodeURIComponent('WQFuk9Py2VC3jsCrPDJ69xtsj33MiXAElBzQw3TSHqOOyDYL0vRJELrB1hTTW3NG0A54kE2FVueuHMkQd8JveYb92eV+dYH4IUrrvJdP5Of9HGi+5i6GT11nL0dAv/ecyP4sB0jM8rVZzyS8imLarvPUvPuFPvvB8nwYXZ2ZKon4IPjmtC8SPftaw0PSuZKMg735hLCraeAE3lMQbRwiY7yMHKmUHDALG1Ky+HBDiK20vAGEIMwzPwKZmhbBXRGiHzEiXiRk4JgaVyp6N66EALao7VOE7UTzJLs='),
                'encrypted'         : 'true',
                'video_title'       : "some cool vids title here",
                'embed_js'          : "",
                'prerollRepeat'     : "false"
            };

how can i use preg_match to extract the value for video_title so that it become:

echo $videoTitle;

will output: some cool vids title here

and for video_url will output:

WQFuk9Py2VC3jsCrPDJ69xtsj33MiXAElBzQw3TSHqOOyDYL0vRJELrB1hTTW3NG0A54kE2FVueuHMkQd8JveYb92eV+dYH4IUrrvJdP5Of9HGi+5i6GT11nL0dAv/ecyP4sB0jM8rVZzyS8imLarvPUvPuFPvvB8nwYXZ2ZKon4IPjmtC8SPftaw0PSuZKMg735hLCraeAE3lMQbRwiY7yMHKmUHDALG1Ky+HBDiK20vAGEIMwzPwKZmhbBXRGiHzEiXiRk4JgaVyp6N66EALao7VOE7UTzJLs=

my php look like this:

$pagex = $this->page;
$patternx = '/("video_title":")(.*?)(")/i';
if(preg_match($patternx,$pagex,$arx)){  
    $key = $arx[2];
}               


$page = $this->page;
$pattern = '/("video_url":")(.*?)(")/i';
if(preg_match($pattern,$page,$ar)){ 
$link = $ar[2];
}

and it does not work. It show nothing.

Upvotes: 1

Views: 131

Answers (2)

Imanuel
Imanuel

Reputation: 3667

For video_title (allows escaped " in the title):

'video_title'\s*:\s*"(.*?)*(?<!\\)"

In PHP:

$pattern='\'video_title\'\s*:\s*"(.*?)*(?<!\\)"';

And for url:

'video_url'\s*:\s*encodeURIComponent\('([^']*)'\)

And:

$pattern='\'video_url\'\s*:\s*encodeURIComponent\(\'([^\']*)\'\)';

Upvotes: 0

kittycat
kittycat

Reputation: 15044

$string = "'disablePauseroll'  : disablePauseroll,
            'video_url'         : encodeURIComponent('WQFuk9Py2VC3jsCrPDJ69xtsj33MiXAElBzQw3TSHqOOyDYL0vRJELrB1hTTW3NG0A54kE2FVueuHMkQd8JveYb92eV+dYH4IUrrvJdP5Of9HGi+5i6GT11nL0dAv/ecyP4sB0jM8rVZzyS8imLarvPUvPuFPvvB8nwYXZ2ZKon4IPjmtC8SPftaw0PSuZKMg735hLCraeAE3lMQbRwiY7yMHKmUHDALG1Ky+HBDiK20vAGEIMwzPwKZmhbBXRGiHzEiXiRk4JgaVyp6N66EALao7VOE7UTzJLs='),
            'encrypted'         : 'true',
            'video_title'       : \"some cool vids title here\",";

preg_match("/encodeURIComponent\('([^']+)/", $string, $match);

echo $match[1]; // URL

preg_match('/video_title[^"]+"([^"]+)/', $string, $match);

echo $match[1]; // Title

This will extract the text string between the ' quotes within encodeURIComponent() and the 2nd will extract the text within the " quotes for video_title

Upvotes: 1

Related Questions