0

I have a text.

\cf1\i ( Wong v. Tenneco, Inc. (1985) 39 Cal.3d 126, 135 [216 Cal.Rptr. 412, 702 P.2d 570]; Knodel v. Knodel (1975) 14 Cal.3d 752, 765, fn. 15 [122 Cal.Rptr. 521, 537 P.2d 353].)\cf0\i0 This approach was enunciated long ago by Justice Cardozo, who explained that courts will not refuse to enforce a foreign cause of action unless application of the foreign law 'would violate some fundamental principle of justice, some prevalent conception of good morals, some deep-rooted tradition of the common weal.' \cf1\i ( Loucks v. Standard Oil Co. (1918) 224 N.Y. 99, 111 [120 N.E. 198, 202].)\cf0\i0 California case, \cf1\i Bryant v. Mead (1851) 1 Cal. 441\cf0\i0

here: I have

$1st  = "\cf1\i"; 
$2nd = "\cf0\i0";

these two substring. I have to find a string starting with $1st and ending with $2nd in this text though this text is dynamic, and I don't know how many times these two substrings will appear.

I did this : $between=substr($str, strpos($str, $1st) + 6, strpos($str, $2nd) - strpos($str, $1st) + 1);

but I give only the first string.

How can I get all the strings starting with $1st and ending with $2nd in this text?

1
  • do my homework please... > No, show code you tried, and where you failed. -1 Commented Nov 5, 2011 at 9:31

3 Answers 3

1

From: http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

$fullstring = "this is my [tag]dog[/tag]";
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");

echo $parsed; // (result = dog)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your reply. but if I want to find those "[tag]" and "[/tag]" more than one In that same text than what will I need to do.
I wrote a function for this awhile back, but don't have the source code on me at the moment. You can easily modify this function to have it keep searching, and return an array of results.
0

I just modify your code and find the answere. here is my code:

$fullstr = "this is my [tag]dog[/tag].this is my [tag]this is my dog[/tag].this is my [tag]this is my dog[/tag]";
$start = "[tag]";
$end = "[/tag]";

    $first = substr_count($fullstr, $start);
    $second = substr_count($fullstr, $end);

    function get_string_between($string, $start, $end)
     {
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }


    $i=0;

    while($i<=$first)
    {
        $occur = strpos($fullstr, $val5, $i);
        echo $between = get_string_between($fullstr, $val7, $val5);

        $fullstr= substr($fullstr, $occur, 100000);

     $i++;
    }

Comments

0

Use:

<?php

$str = "...server daemon started with pid=6849 (parent=6848).";
$from = "pid=";
$to = "(";

echo getStringBetween($str,$from,$to);

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.