Extract string between two characters
Snippet for extracting a string between two characters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function extractStringBetween($cFirstChar, $cSecondChar, $sString)
{
preg_match_all("/\\".$cFirstChar."(.*?)\\".$cSecondChar."/", $sString, $aMatches);
return $aMatches[1];
}
$sMyString = "Bitte alle eingeklammerten Begriffe extrahieren: [Symfony] [CodeIgniter] #PHP# [Zend]";
var_dump(extractStringBetween("[", "]", $sMyString));
/*
* Ausgabe:
*
array(3)
{
[0]=>
string(7) "Symfony"
[1]=>
string(11) "CodeIgniter"
[2]=>
string(4) "Zend"
}
*/
X
Url: http://sklueh.de/2012/09/text-zwischen-zwei-zeichen-extrahieren/
Language: PHP | User: sklueh | Created: Sep 24, 2013