试试这个:
$string = 'I have a mango which <price>1111</price> 10, a banana which <price>3333</price> 15 and an apple which <price>5555</price> 20. So the total amount spent is Rs 45';
$pattern = '/\<price\>(.*?)\<\/price\>/';
$matches = array();
preg_match_all($pattern, $string, $matches);
var_dump($matches);
var_dump($matches[1][2]);
结果是:
array(2) {
[0]=>
array(3) {
[0]=>
string(19) "<price>1111</price>"
[1]=>
string(19) "<price>3333</price>"
[2]=>
string(19) "<price>5555</price>"
}
[1]=>
array(3) {
[0]=>
string(4) "1111"
[1]=>
string(4) "3333"
[2]=>
string(4) "5555"
}
}
string(4) "5555" // <-- that's what you want
您是否尝试过的东西?请包括您尝试过的代码 - 阅读[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask) – aslawin
谢谢您的建议。我做过尝试,但只能获取第一次出现,即。 (ABCDEF)。 $ input =“我有一个芒果,其成本(abcdef)10卢比,一个香蕉成本(ghijk)15卢比和一个苹果花费(lmnop)卢比20。所以花费的总金额是45卢比。 preg_match('〜cost(。*?)Rs〜',$ input,$ output); –