Hat jemand Lust, mir einen Regex zu bauen? Wäre mir eine große Hilfe:
Gib aus einem String alle Strings zwischen zwei Gradzeichen (° bzw. \%7D) zurück, bei denen der Abstand zwischen den Gradzeichen kleiner ist als 20 Zeichen.
Also
.. Lorem Ipsum Dolor Sit Amet, °display° Convivium sit amet
=> Match "display"
° .. Lorem Ipsum Dolor Sit Amet, Convivium sit amet°
=> Kein Match
Das gehört ja eigentlich in die Projekthilfe :D. Aber auf die Schnelle:
$string_1 = '.. Lorem Ipsum Dolor Sit Amet, °display° Convivium sit amet ';
$string_2 = '° .. Lorem Ipsum Dolor Sit Amet, Convivium sit amet° ';
preg_match ('/°(\S.*?)°/',$string_1, $hits);
print_r($hits);
preg_match ('/°(\S.*?)°/',$string_2, $hits);
print_r($hits);
Peter
Das gehört ja eigentlich in die Projekthilfe :D.
Das stimmt, beachte ich beim nächsten Mal.
Ebenfalls vielen Dank!
fireweasel
22-10-2009, 20:42
Mist! Zu spät.
Ich hätte den hier vorgeschlagen:
'/°.{0,19}?°/s'
in preg_match_all() verpackt:
$haystacks = array (
'.. Lorem Ipsum Dolor Sit Amet, °display° Convivium sit amet',
'° .. Lorem Ipsum Dolor Sit Amet, Convivium sit amet°',
// Mindestabstand (0)
'°°',
// Maximalabstand (19)
"all those who remember the war they won't °forget what they've° seen",
// preg_split() waere eine Alternative, falls nicht
'°abcd°war-das-so-gewollt?°efgh°',
);
foreach ($haystacks as $haystack) {
$hits = preg_match_all(
'/(?<=°).{0,19}?(?=°)/s', $haystack, $hits, PREG_PATTERN_ORDER
) ? $hits[0] : NULL;
var_dump($hits);
}