Moumita
Moumita

Reputation: 360

How to find a string between two substring?

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?

Upvotes: 0

Views: 1455

Answers (3)

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

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

?>

Upvotes: 0

Moumita
Moumita

Reputation: 360

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++;
    }

Upvotes: 0

Brad
Brad

Reputation: 163272

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)

Upvotes: 1

Related Questions