-2

Possible Duplicate:
Get content between two strings PHP

I am trying to create a php function that will return an array of all occurrences of text between two substrings. I am useless at regular expression and would find this very handy, I'm sure there is already a very nice one out there but I can't find it.

    function getAllBetweenStr($input, $start, $end)
    {
         //DO STUFF

         //RETURN ARRAY
    }


$inputStr ="<a href='http://formaco4/en/abc' class='currentLink' hreflang='en'>abc1/a>
<a href='http://formaco4/en/abc/b1'  hreflang='en'>abc2</a>
<a href='http://formaco4/en/abc/b2'  hreflang='en'>abc3</a>
<a href='http://formaco4/en/abc/b3'  hreflang='en'>abc4</a>";


$linkStr = array();
$linkStr = getAllBetweenStr($inputStr, "http://formaco4/en/'", "'  hreflang")

The return array would be like this Array([0] => "abc/b1", [1] => "abc/b2", ... , [n] => "gingers/rule")

I have this function already if it helps:

    function getBetweenStr($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);
    }
Community
  • 1
  • 1
Tristanisginger
  • 2,181
  • 4
  • 28
  • 41
  • 4
    and this is actually what preg_match / preg_match_all (in your case) is made for ... – F. Müller Oct 05 '12 at 13:25
  • you define $linkstr as a array. Then you define $linkstr as a string (returning the substr) when you want to have it as an array you have to define it as $link[]. – Jordi Kroon Oct 05 '12 at 13:27
  • *(related)* [How to parse and proccess HTML with PHP](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php/3577662#3577662) – Gordon Oct 05 '12 at 14:30

5 Answers5

1

I believe something like this will work - It uses preg_match_all to capture anything between the two $start and $end strings.

function getBetweenStr($string, $start, $end)
{
    preg_match_all( '/' . preg_quote( $start, '/') . '(.*?)' . preg_quote( $end, '/') . '/', $string, $matches);
    return $matches[1];
}
nickb
  • 59,313
  • 13
  • 108
  • 143
0

Mine doesn't use the preg_match function. Hopefully you will find this useful

Try this, I've also provided a better function for you (: :

function stribet($inputstr, $delimiterLeft, $delimiterRight) {
    $posLeft = stripos($inputstr, $delimiterLeft) + strlen($delimiterLeft);
    $posRight = stripos($inputstr, $delimiterRight, $posLeft);
    return substr($inputstr, $posLeft, $posRight - $posLeft);
}

$links = array("<a href='http://formaco4/en/abc' class='currentLink' hreflang='en'>abc1/a>", "<a href='http://formaco4/en/abc/b1'  hreflang='en'>abc2</a>");

foreach ($links as $linkstr){
$linkstr = stribet($linkstr, "http://formaco4/en/'", "'  hreflang='");
//echo $linkstr;
$data[] = $linkstr;
}

print_r($data);

if you would just like to stick to normal string function, try exploding <a then use the foreach loop to get your data.

Something like

$inputStr ="<a href='http://formaco4/en/abc' class='currentLink' hreflang='en'>abc1/a>
<a href='http://formaco4/en/abc/b1'  hreflang='en'>abc2</a>
<a href='http://formaco4/en/abc/b2'  hreflang='en'>abc3</a>
<a href='http://formaco4/en/abc/b3'  hreflang='en'>abc4</a>";
$inputArray = explode ("<a", $inputStr);
  foreach ($inputArray as $linkstr){
    $linkstr = stribet($linkstr, "http://formaco4/en/'", "'  hreflang='");
    //echo $linkstr;
    $data[] = $linkstr;
    }

    print_r($data);
MrYanDao
  • 1,253
  • 1
  • 15
  • 27
0

It's really worth learning regular expressions. They're very powerful and useful:

preg_match_all('|'.preg_quote($start).'(.*?)'.preg_quote($end).'|', $input, $linkStr);

In your example this would build a regular expression of:

|http:\/\/formaco4\/en\/\(.*?)  hreflang|

The parens () tell it to capture that part of the string. The ".*?" within the parens instruct it to catch all characters up to what follows.

Matt S
  • 14,976
  • 6
  • 57
  • 76
0

Try this one (untested):

function find_occurencies($start, $search, $end)
{
    $start = stripslashes($start);
    $end = stripslashes($end);
    $search = stripslashes($search);

    $pattern = '%'. $start . '([\s\w]+)?' . $end . '%';
    preg_match_all($pattern, $search, $matches);

    // Print the matches (should)
    echo '<pre>';
    print_r($matches);
    echo '</pre>';
}

Hope it works.

F. Müller
  • 3,969
  • 8
  • 38
  • 49
0
<?php

function getAllBetweenStr($string, $start, $end)
    {
        $array_to_return = array();
        for($i=0; $i<count(explode($start,$string))-1; $i++) {
        $str_0 = explode($start,$string);
        $str_1 = explode($end, $str_0[1]);
        $array_to_return[$i] = $str_1[0];
        }
        return $array_to_return;
    }

$inputStr ="<a href='http://formaco4/en/abc' class='currentLink' hreflang='en'>abc1</a>
<a href='http://formaco4/en/abc/b1'  hreflang='en'>abc2</a>
<a href='http://formaco4/en/abc/b2'  hreflang='en'>abc3</a>
<a href='http://formaco4/en/abc/b3'  hreflang='en'>abc4</a>";
$linkStr = getAllBetweenStr($inputStr, "hreflang='en'>", "</a>");
print_r ($linkStr);
?>
chokrijobs
  • 761
  • 1
  • 6
  • 10