1

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.

1

5 Answers 5

2

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"
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well i have the concept. But that's what I cannot do :(
Edits above. This might help :)
you helped me alot with this function. Can I buy you a coffe?
Thank you, you can always send me a coffee though PayPal if you'd like, [email protected]
Thank you, i got the donation, just couldn't work out if it was from you :) glad i could help you!
2

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
)

4 Comments

As an addition: off course <b> and </b> could be changed to your variables $start and $end.
Isn't it supposed to display only the two values, can we avoid that sort of dublication? :) Thanks.
Sorry, but I'm not sure what duplication you are trying to avoid. My answer gives you the result you requested.
Sorry a mistake of mine. Thank you for your efforts.
0

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.

1 Comment

That's not the point. I want to be able to fetch some part of a wegpages content <b> is there only for illustrative purposes.
0

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" } 

Comments

0

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

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.