1

The string is:

<div class="item-inner clearfix"> <div class="item-img have-additional clearfix"> <div class="cart-wrap"> <button title="Sәbәtә әlavә et" class="btn-cart" onclick="setLocation('https://az.from.ae/az/checkout/cart/add/uenc/aHR0cHM6Ly9hei5mcm9tLmFlL2F6L2NhdGFsb2dzZWFyY2gvcmVzdWx0L2luZGV4Lz9xPWlwaG9uZSs1cysxNitnYiZjYXQ9MzkmZGlyPWFzYyZvcmRlcj1wcmljZSZsaW1pdD0yNA,,/product/412/form_key/XUXNITbPm6bBkzTQ/')" data-toggle="tooltip" data-original-title="Sәbәtә әlavә et">Sәbәtә әlavә et</button> </div> <div class="item-img-info"> <a href="https://az.from.ae/az/apple-iphone-5s-16-gb-4g-lte-wi-fi-space-gray" title="Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray) Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)" class="product-image"> <span class="img-main"> <img width="270" height="270" src="https://az.from.ae/media/product/78d/apple-iphone-5s-16-gb-4g-lte-wi-fi-space-gray-buy-apple-iphone-5s-cbf.jpg" alt="Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray) Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)"/> </span> </a> </div> </div> <div class="item-info"> <div class="info-inner"> <div class="item-title"> <a href="https://az.from.ae/az/apple-iphone-5s-16-gb-4g-lte-wi-fi-space-gray" title="Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)"> Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray) </a> </div> <div class="item-content"> <div class="item-price"> <div class="price-box"> <span class="regular-price" id="product-price-4128672363671444684686"> <span class="price">AZN  522</span> </span> </div> </div> <p class="no-rating"><a href="https://az.from.ae/az/apple-iphone-5s-16-gb-4g-lte-wi-fi-space-gray/reviews/#review-form">Rəyinizi bildirin</a></p> </div> </div> </div></div> 

My string also be like this:

<div class="item-inner clearfix"> <div class="item-img have-additional clearfix"> <div class="cart-wrap"> <span class="btn-cart availability out-of-stock"><span>Out of stock</span></span> </div> <div class="item-img-info"> <a href="https://az.from.ae/az/apple-iphone-5s-64-gb-4g-lte-wi-fi-gold" title="Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold) Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)" class="product-image"> <span class="img-main"> <img width="270" height="270" src="https://az.from.ae/media/product/d3e/apple-iphone-5s-64-gb-4g-lte-wi-fi-gold-buy-apple-iphone-5s-2dd.jpg" alt="Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold) Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)"/> </span> </a> </div> </div> <div class="item-info"> <div class="info-inner"> <div class="item-title"> <a href="https://az.from.ae/az/apple-iphone-5s-64-gb-4g-lte-wi-fi-gold" title="Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)"> Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold) </a> </div> <div class="item-content"> <div class="item-price"> <div class="price-box"> <span class="regular-price" id="product-price-4166746673391444684602"> <span class="price">AZN  633</span> </span> </div> </div> <p class="no-rating"><a href="https://az.from.ae/az/apple-iphone-5s-64-gb-4g-lte-wi-fi-gold/reviews/#review-form">Rəyinizi bildirin</a></p> </div> </div> </div></div> 

And my code is:

preg_match('/" title="(.*?)">/mis', $product, $names);

Whi it dont return Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)?

  • It's [not a good idea to parse HTML with regex](http://stackoverflow.com/a/1732454/2370483). Consider [DOMDocument](http://php.net/manual/en/class.domdocument.php) instead – Machavity Oct 12 '15 at 23:38
  • I believe that to extract information from HTML, regex is more useful than having to process the whole document to find just 1~5 lines. HTML Parsers should be used for more complex operations, not simple searches like this. – GGG Oct 13 '15 at 00:37

2 Answers2

0

Try this pattern preg_match('#title="([^"]*)"#', $product, $names);

$names[1] will contain what you need

Also - this should help you with debugging this sort of things

https://regex101.com/

Stas Parshin
  • 1,458
  • 1
  • 16
  • 21
  • i edited my question please check again. the $product string can be first one or second one. I need to get product name – user2398907 Oct 12 '15 at 21:52
  • preg_match('#title="([^"]*)"#', $product, $names); - try this one instead – Stas Parshin Oct 12 '15 at 22:09
  • try the same pattern with preg_match_all() - but you'll have to choose the right match somehow, if you are trying to parse some site - perhaps you should use something like simplehtmldom instead of preg – Stas Parshin Oct 12 '15 at 22:27
0

Just use preg_match_all("/\" title=\"(.*?)\"/im", $html, $res), $res[1] will contain what you're looking for.
Result:

Array
(
    [0] => Array
        (
            [0] => " title="Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray) Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)"
            [1] => " title="Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)"
            [2] => " title="Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold) Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)"
            [3] => " title="Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)"
        )

    [1] => Array
        (
            [0] => Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray) Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)
            [1] => Apple iPhone 5S (16 GB, 4G LTE + Wi-Fi, Space Gray)
            [2] => Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold) Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)
            [3] => Apple iPhone 5S (64 GB, 4G LTE + Wi-Fi, Gold)
        )

)

And I recommend you use http://regexr.com/ to test your regular expressions.

GGG
  • 640
  • 1
  • 9
  • 27