Here is a line.

<a class="history-item product" title="USB Keyboard Leather Cover Case Bag for 7" Tablet PC MID PDA VIA 8650,Free Shipping + Drop Shipping" href="http://www.aliexpress.com/product-gs/495886336-USB-Keyboard- Leather-Cover-Case-Bag-for-7-Tablet-PC-MID-PDA-VIA-8650- Free-Shipping-wholesalers.html">USB Keyboard Leather Cover Case Bag for 7" Tablet PC MID PDA VIA 8650,Free Shipping + Drop Shipping</a> 

From it you need to pull out the title and the link itself, take between quotes with the help of the named parts of the template. Here is just the title and still does not work .. how to write correctly?

  $pattern7='#<a class="history-item product" title="(?<Cost2>.*)".*#Ui'; 

    2 answers 2

    Judging by the link, the syntax is not met, the double quotation for inches makes the opening opening, and the Tablet PC MID PDA VIA 8650, Free Shipping + Drop Shipping with incomprehensible text inside the tag attributes, this regular expression applies to this text:

     /<a[^>]*title=\"(?<title>[^>]*)\"\s*href=\"(?<url>[^\"]*)\"/i 

    If it were not for this quotation it would be more correct:

     /<a[^>]*title=\"(?<title>[^\"]*)\"\s*href=\"(?<url>[^\"]*)\"/i 

    Your regular expression is correct and it takes this data [USB Keyboard Leather Cover Case Bag for 7] this is just before this quote that denotes inches

    The working code for processing this text is:

     $str='<a class="history-item product" title="USB Keyboard Leather Cover Case Bag for 7" Tablet PC MID PDA VIA 8650,Free Shipping + Drop Shipping" href="http://www.aliexpress.com/product-gs/495886336-USB-Keyboard-Leather-Cover-Case-Bag-for-7-Tablet-PC-MID-PDA-VIA-8650- Free-Shipping-wholesalers.html">USB Keyboard Leather Cover Case Bag for 7" Tablet PC MID PDA VIA 8650,Free Shipping + Drop Shipping</a>'; preg_match('/<a[^>]*title=\"(?<title>[^>]*)\"\s*href=\"(?<url>[^\"]*)\"/i',$str, $match); echo $match['1'];//вернет текст в title echo $match['title']; // вернет текст в title echo $match['2'];//вернет ссылку echo $match['url'];//вернет ссылку 
    • Thanks, but I didn’t understand what this means <a [^>] * ^ - is this the inventive, does it turn out a after it is not> repeated 0 or more times? Why is this construction so necessary? You can do without it .. - Vyacheslav Potapov
    • This allows you to bypass other attributes in the tag facing the title, because the title will not always be immediately there or the class = "history-item product" will not always (protect against data loss), and this design allows you to search in the description of this tag, if the description does not have this attribute, then preg_match will skip it as not matching the pattern. - kingNothing
    • If you use sets of any characters. * As <a. * Title ..., then you can run into <a class = "lalala"> </a> <p title = "bububu" ... </ p> ... <a href = "gg" ..., which is not what is desired. [^>] * such a construction guarantees a result in any case, whether there are attributes in front of the title or not in the tag description. - kingNothing

    Here you can try: http://writecodeonline.com/php/

      $array; $search_string = '<a class="history-item product" title="USB Keyboard Leather Cover Case Bag for 7" Tablet PC MID PDA VIA 8650,Free Shipping + Drop Shipping" href="http://www.aliexpress.com/product-gs/495886336-USB-Keyboard- Leather-Cover-Case-Bag-for-7-Tablet-PC-MID-PDA-VIA-8650- Free-Shipping-wholesalers.html">USB Keyboard Leather Cover Case Bag for 7" Tablet PC MID PDA VIA 8650,Free Shipping + Drop Shipping</a>'; preg_match('/(?<=[t][i][t][l][e][=]["]).+?(?=["])/',$search_string, $array); echo '<br>Здесь title: '; $title = $array[0]; echo $title; $array2; preg_match('/(?<=[>]).+?(?=[<])/',$search_string, $array2); echo '<br>Здесь ссылка: '; $link_a = $array2[0]; echo $link_a;