Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $string = "[A][B][C]test1[/B][/C][/A] [A][B]test2[/B][/A] test3" ;
- $string = preg_replace('/<A[^>]*>([sS]*?)</A[^>]*>/', '', strtr($string, array("["=>"<","]"=>">")));
- $string = trim($string);
- var_dump($string);
- string 'test3' (length=5)
- $string = "[A][B][C]test1[/B][/C][/A] [A][B]test2[/B][/A] test3";
- $found = ''; // this will be equal to test3
- $boom = explode('[/A]', $string);
- foreach ($boom as $val) {
- if (strpos($val, '[A] ') !== false) { $found = $val; break; }
- }
- echo $found; // test3
- $str = 'test0[A]test1[B][C]test2[/B][/C][/A] [A][B]test3[/B][/A] test4';
- $matches = array();
- // Find and remove the unneeded strings
- $pattern = '/([A]|[B]|[C])[^[]*([A]|[B]|[C])[^[]*([A]|[B]|[C])([^[]*)([/A]|[/B]|[/C])[^[]*([/A]|[/B]|[/C])[^[]*([/A]|[/B]|[/C])/';
- preg_match_all( $pattern, $str, $matches );
- $stripped_str = $str;
- foreach ($matches[0] as $key=>$matched_pattern) {
- $matched_pattern_str = str_replace($matches[4][$key], '', $matched_pattern); // matched pattern with text between A,B,C tags removed
- $stripped_str = str_replace($matched_pattern, $matched_pattern_str, $stripped_str); // replace pattern string in text with stripped pattern string
- }
- // Get required strings
- $pattern = '/([A]|[B]|[C]|[/A]|[/B]|[/C])([^[]+)([A]|[B]|[C]|[/A]|[/B]|[/C])/';
- preg_match_all( $pattern, $stripped_str, $matches );
- $required_strings = array();
- foreach ($matches[2] as $match) {
- if (trim($match) != '') {
- $required_strings[] = $match;
- }
- }
- // Special case, possible string on start and end
- $pattern = '/^([^[]*)([A]|[B]|[C]).*([/A]|[/B]|[/C])([^[]*)$/';
- preg_match( $pattern, $stripped_str, $matches );
- if (trim($matches[1]) != '') {
- $required_strings[] = $matches[1];
- }
- if (trim($matches[4]) != '') {
- $required_strings[] = $matches[4];
- }
- print_r($required_strings);
Add Comment
Please, Sign In to add comment