Jump to content

Help searching string


rdrews

Recommended Posts

I need a function that will search a string and return the contents of the string between two other strings.  For example:

 

$str = "1234567 this is a test and I want this text 999999";

$start = "1234567";

$end = "999999";

 

$textIWant = " this is a test and I want this text ";

 

So basically I have a huge string and I want to search it for some starting string and copy all the contents between the starting string and a defined ending string into another variable.

 

Can this be done easily?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/184990-help-searching-string/
Share on other sites

Lots of ways to do it, here's one.

 

$start = preg_quote('1234567', '#');
$end =  preg_quote('999999', '#');
$str = "1234567 this is a test and I want this text 999999";

preg_match("#{$start}(.*?){$end}#s", $str, $out);

print_r($out);

 

Cags!!!  Thanks for the help again.  I'm still working through the problem I was having a few days ago and I'm using both of your solutions to get through it.  I think I've finally got it working now.  Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.