Advertisement
mbis

Remove string with REGEX

Jan 18th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. function pm_get_matching_group() {
  2.     return '(?:4-4s|5-5s-se|5c|6-6s|6-plus-6s-plus|7-et-7-plus|7-7-plus|8-et-8-plus|8-8-plus|x-xs|xs-max|xr?)';
  3. }
  4.  
  5. function pm_filter_product_permalinks($permalink, $slug, $post, $native_slug, $native) {
  6.     if(!empty($post->post_type) && $post->post_type == 'product') {
  7.         $matching_group = pm_get_matching_group();
  8.  
  9.         // $permalink = preg_replace("/([^\/]+)-coque-iphone-{$matching_group}(\/[^\/]+)$/", '$1$3', $permalink);
  10.         // $permalink = preg_replace("/coque-iphone-{$matching_group}-([^\/]+)$/", '$2', $permalink);
  11.  
  12.         $permalink = preg_replace("/([^\/]+)?(coque-iphone-{$matching_group})([^\/]+)?(\/[^\/]+)$/", '$1$3$4', $permalink);
  13.  
  14.         $permalink = preg_replace('/([-]+)/', '-', $permalink);
  15.         $permalink = preg_replace('/(\/([-]+)|([-]+)\/)/', '/', $permalink);
  16.     }
  17.  
  18.     return $permalink;
  19. }
  20. add_filter('permalink_manager_filter_default_post_uri', 'pm_filter_product_permalinks', 9, 5);
  21.  
  22. function pm_filter_product_slugs($slug, $post) {
  23.     if(!empty($post->post_type) && $post->post_type == 'product') {
  24.         $matching_group = pm_get_matching_group();
  25.         $slug = preg_replace("/(coque-iphone-{$matching_group})/", '', $slug);
  26.     }
  27.  
  28.     return $slug;
  29. }
  30. add_filter('permalink_manager_filter_default_post_slug', 'pm_filter_product_slugs', 9, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement