This is a simple regular expression for beginners. thank you! This post was last edited by bupabupa166 from 2014-10-3122: 08: 43 & lt; a & nbsp; titlewomen & nbsp; bags & nbsp; data-textwomen & nbsp; bags & nbsp; hrefht is a simple regular expression for beginners. thank you.
This post was last edited by bupabupa166 at 22:08:43
Women bags Backpack WalletBag
Men KeysoolBag
Dress Handbag ShoulderBag
Purse ShoesBag
Man LeatherBag
How can I use regular expressions to match the attribute values of all "data-text" (values in quotation marks ).
Thank you.
------ Solution ----------------------
$str='women bags backpack wallet bag men keysool bag dress handbag shoulder bag purse shoes bag man leather bag';
preg_match_all('/data-text="(.+)"/U',$str,$matches,PREG_PATTERN_ORDER );
var_dump($matches[1]);
array(12) {
[0]=>
string(10) "women bags"
[1]=>
string(8) "backpack"
[2]=>
string(6) "wallet"
[3]=>
string(7) "bag men"
[4]=>
string(11) "keysool bag"
[5]=>
string(5) "dress"
[6]=>
string(7) "handbag"
[7]=>
string(12) "shoulder bag"
[8]=>
string(5) "purse"
[9]=>
string(5) "shoes"
[10]=>
string(7) "bag man"
[11]=>
string(11) "leather bag"
}
------ Solution ----------------------
Write a relatively intuitive one. first, extract the information in the data-text = "..." quotation marks from the single link.
// Match the first half of the hyperlink, for example
Function filterLink ($ data)
{
$ Pat = '#] +?> # Ui ';
If (preg_match_all ($ pat, $ data, $ match ))
{
Foreach ($ match [0] as $ link)
{
// Echo $ link ."
";
FilterData ($ link );
}
}
Else
{
Echo 'matching failed ';
}
}
// Then extract the required information from a single object
Function filterData ($ data)
{
$ Pat = '# data-text = "([^"] + ?) "# I ';
If (preg_match_all ($ pat, $ data, $ match ))
{
Foreach ($ match [1] as $ content)
{
Echo $ content ."
";
}
}
Else
{
Echo 'matching failed ';
}
}
FilterLink ($ str );