Get text between two strings in php

A little but useful function to get text between two specified strings.

It takes three parameters. The source, start and end. And returns the text between start and end delimeters.

  function between($src,$start,$end){
    $txt=explode($start,$src);
    $txt2=explode($end,$txt[1]);
    return trim($txt2[0]);
  }


Example

$str="This is my content from which i want to fetch a block of text between two delimeters. Note that it returns only the first match";

echo between($str, "my", "returns");
Get text between two strings in php Get text between two strings in php Reviewed by JS Pixels on February 26, 2012 Rating: 5

6 comments:

  1. Doesn't work well !!

    change you $str to ...

    'This is my content from which i want to fetch my block of text between two delimeters. Note that it returns only the first match'

    ... and try it again

    ReplyDelete
  2. I have already explained in example that it returns only the first match.

    ReplyDelete
  3. It's working very well for me. Nice. Thanks

    ReplyDelete
  4. Very simple and works like a charm. Thanks!

    ReplyDelete
  5. I've used your script. It works very much pretty. Thanks Hussain.

    ReplyDelete

Altaf Web. Powered by Blogger.