字符串是:
<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>
我的字符串也是这样的:
<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>
我的代码是:
preg_match('/" title="(.*?)">/mis', $product, $names);
它不会返回 Apple iPhone 5S(16 GB,4G LTE + Wi-Fi,Space Grey)?
答案 0 :(得分:0)
尝试此模式preg_match('#title="([^"]*)"#', $product, $names);
$names[1]
将包含您需要的内容
此外 - 这应该可以帮助您调试此类事情
答案 1 :(得分:0)
只需使用preg_match_all("/\" title=\"(.*?)\"/im", $html, $res)
,$res[1]
就会包含您要查找的内容。
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)
)
)
我建议您使用http://regexr.com/来测试正则表达式。