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.
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");
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
Reviewed by JS Pixels
on
February 26, 2012
Rating:
Doesn't work well !!
ReplyDeletechange 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
I have already explained in example that it returns only the first match.
ReplyDeleteIt's working very well for me. Nice. Thanks
ReplyDeleteVery simple and works like a charm. Thanks!
ReplyDeleteI've used your script. It works very much pretty. Thanks Hussain.
ReplyDeletepretty code
ReplyDelete