SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $string = "[A][B][C]test1[/B][/C][/A] [A][B]test2[/B][/A] test3" ;
  2. $string = preg_replace('/<A[^>]*>([sS]*?)</A[^>]*>/', '', strtr($string, array("["=>"<","]"=>">")));
  3. $string = trim($string);
  4. var_dump($string);
  5.    
  6. string 'test3' (length=5)
  7.    
  8. $string = "[A][B][C]test1[/B][/C][/A] [A][B]test2[/B][/A] test3";
  9.  
  10. $found = ''; // this will be equal to test3
  11. $boom = explode('[/A]', $string);
  12.  
  13. foreach ($boom as $val) {
  14.  if (strpos($val, '[A] ') !== false) { $found = $val; break; }
  15. }
  16.  
  17. echo $found; // test3
  18.    
  19. $str = 'test0[A]test1[B][C]test2[/B][/C][/A] [A][B]test3[/B][/A] test4';
  20. $matches  = array();
  21.  
  22. // Find and remove the unneeded strings
  23. $pattern = '/([A]|[B]|[C])[^[]*([A]|[B]|[C])[^[]*([A]|[B]|[C])([^[]*)([/A]|[/B]|[/C])[^[]*([/A]|[/B]|[/C])[^[]*([/A]|[/B]|[/C])/';
  24. preg_match_all( $pattern, $str, $matches );
  25. $stripped_str = $str;
  26. foreach ($matches[0] as $key=>$matched_pattern) {
  27.   $matched_pattern_str  = str_replace($matches[4][$key], '', $matched_pattern); // matched pattern with text between A,B,C tags removed
  28.   $stripped_str = str_replace($matched_pattern, $matched_pattern_str, $stripped_str); // replace pattern string in text with stripped pattern string
  29. }
  30.  
  31. // Get required strings
  32. $pattern = '/([A]|[B]|[C]|[/A]|[/B]|[/C])([^[]+)([A]|[B]|[C]|[/A]|[/B]|[/C])/';
  33. preg_match_all( $pattern, $stripped_str, $matches );
  34. $required_strings = array();
  35. foreach ($matches[2] as $match) {
  36.   if (trim($match) != '') {
  37.     $required_strings[] = $match;
  38.   }
  39. }
  40.  
  41. // Special case, possible string on start and end
  42. $pattern = '/^([^[]*)([A]|[B]|[C]).*([/A]|[/B]|[/C])([^[]*)$/';
  43. preg_match( $pattern, $stripped_str, $matches );
  44. if (trim($matches[1]) != '') {
  45.   $required_strings[] = $matches[1];
  46. }
  47. if (trim($matches[4]) != '') {
  48.   $required_strings[] = $matches[4];
  49. }
  50.  
  51. print_r($required_strings);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top