skip to Main Content

Please check the following paragraph for example.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad *minim veniam*, quis nostrud exercitation ullamco laboris *nisi ut* aliquip ex ea commodo consequat.

In the above paragraph "minim veniam" and "nisi ut" are inside asterisk. I want to remove those 2 sections from the above text. How to do it using PHP string handling? Please help.

I tried many thing with str_replace and preg_replace, but not working. Everytime it removes only the asterisk only. Not the text within it.

2

Answers


  1. <?php
    $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad *minim veniam*, quis nostrud exercitation ullamco laboris *nisi ut* aliquip ex ea commodo consequat.";
    $result = preg_replace('/*[^*]**/', '', $text);
    echo $result;
    ?>
    

    have you tried this regex?

    Login or Signup to reply.
  2. I think the preg_replace will be the function that will help you.

    The pattern that can be used here is: /*.*?*/

    Example implementation:

    $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad *minim veniam*, quis nostrud exercitation ullamco laboris *nisi ut* aliquip ex ea commodo consequat.";
    
    $pattern = "/*.*?*/";
    $result = preg_replace($pattern, '', $text);
    
    echo $result; // The $result variable is your desired result
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search