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
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);

Link to comment
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.