我有一个字符串:
$var = explode(",", $var);
当我使用爆炸时:
[Item 1]
[Item 2]
[Item, 3]
这也会抛出方括号内的逗号。
我想回复:
intent.setClassName(CallingActivity.this, activityToLaunch.getComponentName().getClassName());
运行我正在使用的foreach(){}语句。有什么想法吗?
答案 0 :(得分:6)
private func _fetchRecordChangesFromCloud() {
if !_fetching {
// this is the first and only time this code is called in GCD recusion
// we clean the caches we use to collect the results of the fetch
// so we can then save the record in the correct order so references can be created
_fetchedModifiedRecords = []
_fetchedDeletedRecordIDs = []
// mark fetching has started
_fetching = true
}
let operation = CKFetchRecordChangesOperation(recordZoneID: _customRecordZoneID, previousServerChangeToken: _serverChangeToken)
operation.recordChangedBlock = { (record: CKRecord?) in
if let record = record {
println("Received record to save: \(record)")
self._fetchedModifiedRecords.append(record)
}
}
operation.recordWithIDWasDeletedBlock = { (recordID: CKRecordID?) in
if let recordID = recordID {
println("Received recordID to delete: \(recordID)")
self._fetchedDeletedRecordIDs.append(recordID)
}
}
operation.fetchRecordChangesCompletionBlock = {
(serverChangeToken: CKServerChangeToken?, clientChangeToken: NSData?, error: NSError?) -> Void in
if let error = error {
println("Error in fetching record changes: \(error)")
// try again next sync
self._fetchAfterNextSuccessfullSync = true
self._fetching = false
return
}
// fetched records successfuly
println("fetched records successfuly")
if let serverChangeToken = serverChangeToken {
self._serverChangeToken = serverChangeToken
}
if operation.moreComing {
// we need to create another operation object and do it again
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
self._fetchRecordChangesFromCloud()
}
} else {
// we are finally done
// process the fetched records
self._processFetchedRecords()
// save all changes back to persistent store
self._saveBackgroundContext()
// we are done
self._fetching = false
}
}
self._privateDatabase.addOperation(operation)
}
- doh忘记了分隔符已丢失所以这是一个粗略的选项,可以将这些[]重新放入:
$var = "[Item 1],[Item 2],[Item, 3]";
$var = explode("],[", $var);
print_r($var);
可能更好地切换到正则表达式拆分,我会在一秒内写出
最佳性感回答:
<?php
$var = "[Item 1],[Item 2],[Item, 3]";
$var = explode("],[", $var);
foreach ($var as $v){
if(substr($v,0,1)!='['){
$v='['.$v;
}
if(substr($v,-1)!=']'){
$v=$v.']';
}
$out[]=$v;
}
echo '<pre>';
print_r($out);
答案 1 :(得分:0)
它无法正常工作。但是可以用
来完成$var = "[Item 1],[Item 2],[Item, 3]";
$varArr = explode("],", $var);
$newArr= array();
foreach($varArr as $key=>$val)
$newArr[$key] = $val . "]";
echo "<pre>";
print_r($newArr);
echo "</pre>";
答案 2 :(得分:0)
如何使用正则表达式提取这些信息?
$var = "[Item 1],[Item 2],[Item, 3]";
preg_match_all("(\[.+?\])", $var, $result);
print_r($result[0]);