Jump to content

substring/trim between delimeters


jmurch

Recommended Posts

Hi All,

 

Is there a PHP function that will trim the string and leve only the content between two delimiters?

 

ex:

 

whatever_the_function (delimiter, string, delimiter) so the the output is the sting with everthing before the first delimiter deleted and everything after the last delimiter deleted.

 

so that stringstringstringstringxxxxstringstringyyyystring would result in stringstring if the first delim were xxxx and the second were yyyy.

 

Thanks, Jeff

 

Link to comment
https://forums.phpfreaks.com/topic/45634-substringtrim-between-delimeters/
Share on other sites

You could create your own function that uses a series of "explode()" functions to get the result:

<?php
function get_string($delim1,$str,$delim2) 
{
     list($dmy,$tmp) = explode($delim1,$str,2);
     list($tmp,$dmy) = explode($delim2,$tmp,2);
     return ($tmp);
}
$string = 'stringstringstringstringxxxxstringstringyyyystring';
$newstring = get_string('xxxx',$string,'yyyy');
echo $newstring;
?>

 

Ken

 

Ken.

 

Thanks. Your solution is almost perfect. I should have explained it correctly in that I need to keep the delimeters as part of the final string.  The output needs to be:

 

xxxxstringstringyyyy

 

I'm using this to strip out the XML that is outside of the body open and body closed tags so I need to use the body tags as delimeters but keep them as part of the string as well. If this is not possible I guess I could always concatonate them back in after the function has run.

 

Thanks, Jeff

 

Just change the function to:

<?php
function get_string($delim1,$str,$delim2) 
{
     list($dmy,$tmp) = explode($delim1,$str,2);
     list($tmp,$dmy) = explode($delim2,$tmp,2);
     return ($delim1 . $tmp . $delim2);
}
?>

 

Ken

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.