CodeIgniter Forums
preg_match not working we want to choose second title but it choose 1. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: preg_match not working we want to choose second title but it choose 1. (/showthread.php?tid=73864)



preg_match not working we want to choose second title but it choose 1. - aditya5670 - 06-14-2019

Title tag use in same page one is button tag other is p tag  button tag is first title="watch later" other on a href tag title we want to import second a href tag title
source code

[Image: AUrk0j7.png]
[Image: ieuZsHk.png]

[img]hhttps://i.imgur.com/AUrk0j7.png[/img][Image: ufRsX6d.png]






i have tried many things to change /title but it import title is "watch later" i want second title import

            //Title
                    if(preg_match('/title="(.*?)"/', $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';
                    }

how to choose second title under a tag ?
i want to remove watch later title .


RE: preg_match not working we want to choose second title but it choose 1. - donpwinston - 06-14-2019

I believe the only way is to use an "offset" param in preg_match so you search past the first title. Or use PREG_OFFSET_CAPTURE as the flag parameter.


RE: preg_match not working we want to choose second title but it choose 1. - includebeer - 06-14-2019

Use preg_match_all()


RE: preg_match not working we want to choose second title but it choose 1. - jreklund - 06-15-2019

I don't know what more you are fetching from the site. But this will find both url and title for you.

Code:
$re = '@href="/?(video[0-9]+[^>]+)" title="(.*?)">@m';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

And in your case, if you don't want it to do multiple things.
Code:
@p class="title">\s*<a.*?title="(.*?)">@