1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to remove section of text from $string

Discussion in 'General PHP and MySQL Discussions' started by mrb1972, Aug 1, 2011.

  1. mrb1972

    mrb1972 Active Member


    PHP question. Is it possible to remove a section of text from a string ?

    lets say I have a string $example="[caption id="text"] I want to remove this text [/caption] But I want to keep this bit"

    And i want to strip out "[caption id="text"] I want to remove this text [/caption]" from the string, how is this done?

  2. Joshua F

    Joshua F Member

    Are you wanting to replace it with html code, or just remove it completely? Also, if you used that code it'd give errors because you have a double quote inside of a double quote.
  3. mrb1972

    mrb1972 Active Member

    Just remove the code completly, (I know the quote thing shouldnt be there, it was just an example )
  4. James

    James Well-Known Member

    Just use str_replace with no replace parameter.
    str_replace("I want to remove this text"""$example);
  5. mrb1972

    mrb1972 Active Member

    I want to remove everything between [caption & /caption] (including [caption /caption]), I have many text records in my database that have these randomly throughout and each contains different text, I want to automatically go through each text record and strip out [caption /caption] code and all text inbetween
  6. Brogan

    Brogan XenForo Moderator Staff Member

  7. mrb1972

    mrb1972 Active Member

  8. mrb1972

    mrb1972 Active Member

    It wasnt post content I needed to work with but have sussed it out using preg_replace
  9. Jaxel

    Jaxel Well-Known Member

    Off the top of my head....
    $example = preg_replace('#\[caption id=".+?"\].*?\[/caption\]#i', '', $example)

Share This Page