SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Brian911's Avatar
    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Global preg_replace?

    Hi Sitepoint!

    I want to cut out everything from $a to $b in a text variable, eg.
    $a="start";
    $b="end";
    $text="some text start text in between end text later on";

    so $text would be "some text text later on"

    is there a way to do this in php? I know how to do it in perl but php doesnt seem to have a global flag...
    .

  2. #2
    SitePoint Addict melchiorus's Avatar
    Join Date
    Jun 2004
    Location
    Indiana
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try str_replace(). If you want to use regex though, check out preg_replace() and preg_match_all().

    Good luck!
    -Melchior (Stephen Craton)

  3. #3
    SitePoint Enthusiast Brian911's Avatar
    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can I do that with str_replace? it doesnt look like it allows that wildcard-style matching.
    .

  4. #4
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string preg_replace('/\bstart\b.*\bend\b/'''$string); 
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brian911
    I know how to do it in perl but php doesnt seem to have a global flag...
    preg_replace is global unless you specify the $limit parameter.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •