Jump to content

preg_match is not work i want to pick title and duration


aditya56

Recommended Posts

I want to preg_match but now work need title and duration
//Title
if(preg_match('/previewTitle\'>(.*?)</', $match, $matches_title)) {
$video['title'] = htmlspecialchars_decode(strip_tags(stripslashes($matches_title[1])), ENT_QUOTES);
} else {
$this->errors[] = 'Failed to get video title for '.$video['url'].'!';
if (!$this->debug) continue;
else $debug_e[] = 'TITLE';
}

                //Duration


                if(preg_match('/label\'>(.*?)</', $match, $matches_duration)) {
                    $video['duration'] = duration_to_seconds($matches_duration[1]);
                } else {
                    $this->errors[]	= 'Failed to get video duration for '.$video['url'].'!';
                    if (!$this->debug) continue;
                    else $debug_e[] = 'DURATION';
                }

how to choose title ?
how to choose duration ?

I want to preg_match but now work need title and duration
//Title
if(preg_match('/previewTitle\'>(.*?)</', $match, $matches_title)) {
$video['title'] = htmlspecialchars_decode(strip_tags(stripslashes($matches_title[1])), ENT_QUOTES);
} else {
$this->errors[] = 'Failed to get video title for '.$video['url'].'!';
if (!$this->debug) continue;
else $debug_e[] = 'TITLE';
}

                //Duration


                if(preg_match('/label\'>(.*?)</', $match, $matches_duration)) {
                    $video['duration'] = duration_to_seconds($matches_duration[1]);
                } else {
                    $this->errors[]	= 'Failed to get video duration for '.$video['url'].'!';
                    if (!$this->debug) continue;
                    else $debug_e[] = 'DURATION';
                }

how to choose title ?
how to choose duration ?

1560936531-184225-capture.png

 

 

1560936531-184225-capture.png

Link to comment
Share on other sites

  • 2 weeks later...

This is not a use case where you should be using RegEx. A better solution is to use a DOM parser such as Simple HTML Dom. You can easily find elements by searching by multiple parameters - such as tag and class. Using the above content you could get every DOM object for every Div with the class "previewSegment" - which would appear to hold all the contents for each title. Then use the DOM parser to get the text within the "previewTitle" div and the "previewLabel" (which is the runtime). That is how you should be doing this.

But, it can be done via RegEx, I just wouldn't advise it.

$regEx = "#<div class=\".*?previewLabel[^\"]*\">(.*?)</div>.*?<div class=\"previewTitle\">(.*?)</div>#is";
preg_match_all($regEx, $content, $matches, PREG_SET_ORDER);
	$titles = array();
foreach($matches as $match)
{
    $titles[] = array(
        'title' => $match[2],
        'duration' => $match[1]
    );
}
echo "<pre>".print_r($titles, 1)."</pre>";

Example output:

Array
(
    [0] => Array
        (
            [title] => Title of clip 1
            [duration] => 6 min
        )

    [1] => Array
        (
            [title] => Title of clip 2
            [duration] => 12 min
        )
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.