inrob
inrob

Reputation: 5089

extract string within a string php

So I have this script that extracts a given string within a bigger string:

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

So this is the string:

$string= '<b>Sample Sample</b> <b>Sample2 Sample2</b>';

$output = get_string($string, '<b>','</b>');

echo $output;

I really want some help on this because I'm out of ideas. Now when i echo $output I get

Sample Sample

I want to make a change that would display both:

Sample Sample 

Sample2 Sample2

Does any of you guys have any ideas how to modify the function and make it output sort of an array of results $output[0], $output[1]?

Thank you in advance, Wish you a good day.

Upvotes: 1

Views: 3939

Answers (5)

Rasheed
Rasheed

Reputation: 1051

This could help....

function get_string($string, $start, $end = null) {
     $str = "";
     if ($end == null) {
         $end = strlen($string);
     }
     if ($end < $start) {
         return $string;
     }
     while ($start < $end) {
          $str .=$string[$start];
          $start++;
     }
     return $str;
 }

Upvotes: 0

Krycke
Krycke

Reputation: 3186

Modify your function so that as long as while, that function gives you a string, add it to an array, and run the function again with the end of the string.

EDIT:

Didn't want to spell out a correct solution if you happened to want to try it your self. Here's one way to do it, resulting in what you want. I tried to make as few changes to your original post as possible:

function get_string($string, $start, $end){
    $found = array();
    $pos = 0;
    while( true )
    {
        $pos = strpos($string, $start, $pos);
        if ($pos === false) { // Zero is not exactly equal to false...
            return $found;
        }
        $pos += strlen($start);
        $len = strpos($string, $end, $pos) - $pos;
        $found[] = substr($string, $pos, $len);
    }
}

$string = '<b>Sample Sample</b> <b>Sample2 Sample2</b>';

$output = get_string($string, '<b>','</b>');

var_dump( $output );

outputs:

array(2) {
  [0]=>
  string(13) "Sample Sample"
  [1]=>
  string(15) "Sample2 Sample2"
}

Upvotes: 2

Gung Foo
Gung Foo

Reputation: 13558

I hate having to use regex, so here a non-bloatware version:

function get_string($string, $start, $end){
 $results = array();
 $pos = 0;
 do {
   $pos = strpos($string,$start,$pos);
   if($pos === FALSE) break;
   $pos += strlen($start);
   $len = strpos($string,$end,$pos) - $pos;
   $results[] = substr($string,$pos,$len);
   $pos = $pos + $len;
 } while(1);
 return $results;
}

$string= '<b>Sample Sample</b> <b>Sample2 Sample2</b>';

$output = get_string($string, '<b>','</b>');

var_dump($output);

Output:

array(2) { [0]=> string(13) "Sample Sample" [1]=> string(15) "Sample2 Sample2" } 

Upvotes: 0

Rubin Porwal
Rubin Porwal

Reputation: 3845

Looking at your above example the string contains b tags which are HTML tags.

PHP library has a function defined having name strip_tags which will strip HTML tags from string.

So your output string will result into string with plain text and with all HTML tags stripped from the string.

Upvotes: 0

Ross Smith II
Ross Smith II

Reputation: 12179

You might find this much simpler, and easier to understand:

function get_string($string, $start, $end) {
    $start = preg_quote($start, '|');
    $end = preg_quote($end, '|');
    $matches = preg_match_all('|'.$start.'([^<]*)'.$end.'|i', $string, $output);
    return $matches > 0
        ? $output[1]
        : array();
}        

$string= '<b>Sample Sample</b> <b>Sample2 Sample2</b>';
$output = get_string($string, '<b>', '</b>');    
print_r($output);

which outputs:

Array
(
    [0] => Sample Sample
    [1] => Sample2 Sample2
)

Upvotes: 2

Related Questions