Skip to content

Instantly share code, notes, and snippets.

@num8er
Created November 26, 2019 00:09
Show Gist options
  • Save num8er/41d7d4673562e2ece9507fdfe2d05d5d to your computer and use it in GitHub Desktop.
Save num8er/41d7d4673562e2ece9507fdfe2d05d5d to your computer and use it in GitHub Desktop.
<?php
function matchStringsBetweenWrappers($string) {
preg_match_all('/\[N\](.*?)\[\/N\]/u', $string, $matches);
array_walk($matches[0], function(&$item) {
$item = trim($item);
});
return $matches;
}
$string = 'Some text [N]abc_New_New_New[/N] other text [N]ghi_jkl[/N] and other text:;.#{}()[][N]abc_New_[/N].!@';
$matches = matchStringsBetweenWrappers($string);
var_dump($matches);
$string = "في الصيف الماضي ، أنشأ [N]Lego_N [N]Lego_New[/N مجموعة ذات سمة [N]Friends_n.";
$matches = matchStringsBetweenWrappers($string);
var_dump($matches);
$string = "[N]Lego_New[/N] [N]New_Friends_New[/N] [N]Lego_New[/N] ";
$matches = matchStringsBetweenWrappers($string);
var_dump($matches);
$string = "Some [N]Mercedes-Benz[/N], [N]Chick-fil-A[/N] text [N]abc_New_New_New[/N] other text [N]ghi_jkl[/N] and other text:;.#{}()[][N]abc_New[/N].!@ [N]test-&-Test*!'[/N]";
$matches = matchStringsBetweenWrappers($string);
var_dump($matches);
array(2) {
[0]=>
array(3) {
[0]=>
string(22) "[N]abc_New_New_New[/N]"
[1]=>
string(14) "[N]ghi_jkl[/N]"
[2]=>
string(15) "[N]abc_New_[/N]"
}
[1]=>
array(3) {
[0]=>
string(15) "abc_New_New_New"
[1]=>
string(7) "ghi_jkl"
[2]=>
string(8) "abc_New_"
}
}
array(2) {
[0]=>
array(0) {
}
[1]=>
array(0) {
}
}
array(2) {
[0]=>
array(3) {
[0]=>
string(15) "[N]Lego_New[/N]"
[1]=>
string(22) "[N]New_Friends_New[/N]"
[2]=>
string(15) "[N]Lego_New[/N]"
}
[1]=>
array(3) {
[0]=>
string(8) "Lego_New"
[1]=>
string(15) "New_Friends_New"
[2]=>
string(8) "Lego_New"
}
}
array(2) {
[0]=>
array(6) {
[0]=>
string(20) "[N]Mercedes-Benz[/N]"
[1]=>
string(18) "[N]Chick-fil-A[/N]"
[2]=>
string(22) "[N]abc_New_New_New[/N]"
[3]=>
string(14) "[N]ghi_jkl[/N]"
[4]=>
string(14) "[N]abc_New[/N]"
[5]=>
string(21) "[N]test-&-Test*!'[/N]"
}
[1]=>
array(6) {
[0]=>
string(13) "Mercedes-Benz"
[1]=>
string(11) "Chick-fil-A"
[2]=>
string(15) "abc_New_New_New"
[3]=>
string(7) "ghi_jkl"
[4]=>
string(7) "abc_New"
[5]=>
string(14) "test-&-Test*!'"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment