Re: Re: Get the string in between two other strings
| From: | John S. Huggins | Date: | Thu, 17 Oct 2002 13:07:15 +0000 |
| Subject: | Re: Re: Get the string in between two other strings | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-120317@lists.php.net to get a copy of this message | ||
On Thu, 17 Oct 2002, Cameron Cooke wrote:
>-I have looked at these, and they make no sense to me. I tried the substr
>-function, but it all went a bit pear shaped.
Try something like this, you can deal with the linewraps ;)
Reference:
http://au.php.net/manual/en/function.strpos.php
http://au.php.net/manual/en/function.substr.php
http://au.php.net/manual/en/function.strlen.php
<?
$haystackText = "Oh yeah, this is The text before, during, and The text
after";
$firstString = "The text before";
$secondString = "The text after";
$positionFirstString = strpos($haystackText, $firstString);
print("<p>First Position = $positionFirstString</p>");
$positionSecondString = strpos($haystackText, $secondString);
print("<p>Second Position = $positionSecondString</p>");
if ( $positionFirstString < $positionSecondString ) {
$firstLength = strlen($firstString);
$diffBetweenStringPositions = $positionSecondString -
$positionFirstString - $firstLength;
$resultText =
substr($haystackText,$positionFirstString+$firstLength,$diffBetweenStringPositions);
} else {
$secondLength = strlen($secondString);
$diffBetweenStringPositions = $positionFirstString -
$positionSecondString - $secondLength;
$resultText =
substr($haystackText,$positionSecondString+$secondLength,$diffBetweenStringPositions);
}
print("<p>$haystackText<br>$resultText</p>");
?>
>-
>-Cameron
>-
>-
>-"Noodle Snacks" <webmaster@tececo.com> wrote in message
>-news:20021017112721.37399.qmail@pb1.pair.com...
>->
>-> "Cameron Cooke" <cameron@toucanresolve.co.uk> wrote in message
>-> news:20021017110141.26610.qmail@pb1.pair.com...
>-> > Hi,
>-> >
>-> > I need a function to get a string in between two other strings. So you
>-> would
>-> > give the function the string to search and then you pass the first
>-string
>-> > and last string, and the function will return the string in between.
>-> >
>-> > Does anyone have this, or know how to do it. I tried but could not get
>-it
>-> to
>-> > work.
>->
>-> will substr() -
>http://au.php.net/manual/en/function.substr.php suit your
>-> needs?
>->
>-> if not check out:
>->
>-> http://au.php.net/manual/en/ref.regex.php
>->
>-> and
>->
>-> http://au.php.net/manual/en/ref.pcre.php
>->
>->
>-
>-
>-
>---
>-PHP General Mailing List (http://www.php.net/)
>-To unsubscribe, visit: http://www.php.net/unsub.php
>-
**************************************
John Huggins
VANet
jhuggins@va.net
http://www.va.net/
**************************************