var App = App || {};
App = {
getDataFromServer: function(){
var self = this,
deferred = $.Deferred(),
requests = [];
requests.push($.getJSON('request/ajax/url/1'));
requests.push($.getJSON('request/ajax/url/2'));
$.when.apply(jQuery, requests).done(function(xhrResponse) {
return deferred.resolve(xhrResponse.result);
});
return deferred;
},
init: function(){
this.getDataFromServer().done(_.bind(function(resp1, resp2) {
// Do the operations which you wanted to do when you
// get a response from Ajax, for example, log response.
}, this));
}
};
App.init();
Вы можете использовать для этого регулярное выражение:
$matches = array();
preg_match('/identifier\s*(\d+)/', $string, $matches);
var_dump($matches);
\s*
- пробел. (\d+)
соответствует числу.
Вы можете обернуть его в функцию:
function matchIdentifier($string) {
$matches = array();
if (!preg_match('/identifier\s*(\d+)/', $string, $matches)) {
return null;
}
return $matches[1];
}
$string = "identifier 20 j. - cat: text text text aaaa dddd ..... cccc 60' - text, 2008";
$tokens = explode(' ', $string);
$token2 = $tokens[1];
if(is_numeric($token2))
{
$value = (int) $token2;
}
else
{
$value = NULL;
}
Вы можете получить совпадение без захвата подгрупп с помощью оператора \K
и ^
якоря для соответствия слова только в начале строки:
$re = "/^identifier \\K\\d+/";
$str = "identifier 20 j. - cat: text text text aaaa dddd ..... cccc 60' - text, 2008";
preg_match($re, $str, $matches);
echo $matches[0];
Пример программы доступен здесь (PHP v5.5.18).