Ayrıca biçiminin dışında metni gerekli.
function get_delimited_strings($str, $startDelimiter, $endDelimiter) {
$contents = array();
$startDelimiterLength = strlen($startDelimiter);
$endDelimiterLength = strlen($endDelimiter);
$startFrom = $contentStart = $contentEnd = $outStart = $outEnd = 0;
while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos($str, $endDelimiter, $contentStart);
$outEnd = $contentStart - 1;
if (false === $contentEnd) {
break;
}
$contents['in'][] = substr($str, $contentStart, $contentEnd - $contentStart);
$contents['out'][] = substr($str, $outStart, $outEnd - $outStart);
$startFrom = $contentEnd + $endDelimiterLength;
$outStart = $startFrom;
}
$contents['out'][] = substr($str, $outStart, $contentEnd - $outStart);
return $contents;
}
Kullanımı:
$str = "Bore layer thickness [2 mm] instead of [1,25 mm] with [0,1 mm] deviation.";
$cas = get_delimited_strings($str, "[", "]");
verir: Bu yüzden biraz yukarıda raina77ow cevabını değiştirdi
array(2) {
["in"]=> array(3) {
[0]=> string(4) "2 mm"
[1]=> string(7) "1,25 mm"
[2]=> string(6) "0,1 mm"
}
["out"]=> array(4) {
[0]=> string(21) "Bore layer thickness "
[1]=> string(12) " instead of "
[2]=> string(6) " with "
[3]=> string(10) " deviation"
}
}
Belgenizin veri (dize) sayacına ihtiyacınız var mı? –
Daha fazla yakalamak için regex'e devam etmeniz gerekir. Bkz. [Preg_match_all] (http://php.net/manual/en/function.preg-match-all.php) – DarkBee
Preg_match_all'a baktım, ancak bir düzenli ifadeyi nasıl yapacağımı anlayamıyorum. – user3778578