views:

92

answers:

5

With the substr() function i must enter where i want to cut the string by numbers, is there any way of cutting a string by passing words or characters?

Something like this:

<?php
  $string = "Hey there world!";
  magic_substr($string, "there ", "!");
  // returns "world"
?>

Is there a function like that? If so, i've missed something pretty useful, as i've been searching the PHP documentation for a while now. I know i can figure out where to cut the string by using strpos(), but it'd be a lot easier and quicker to cut the string by passing a small part of the string defining where i want to cut it.

EDIT!

There might be smarter ways to do it, so here's what i'm actually trying to do:

$string = "21:0,19:0,20:0,18:0,17:0,25:0,30:0,23:0,31:0,32:0,33:0";
magic_substr($string, "20:", ",");
// returns "0" (i know all of them returns 0 now, but that'll change later)

Any suggestions? Thanks!

A: 

Iv'e never seen a function like this but creating one yourself is easy.

<?php
    function magic_substr($input, $fromStr, $toStr = null) {
        $fromPos = strpos($input, $fromStr) + strlen($fromStr);
        $toPos = strlen($input) - $fromPos;

        if($toStr != null)
            $toPos = strpos($input, $toStr) - $fromPos;

        if($fromPos < 0)
            $fromPos = 0;
        if($toPos < 0)
            $toPos = 0;

        return substr($input, $fromPos, $toPos);
    }
?>
Cory
I knew i could create it easily myself, but i was just looking for a built-in function. It seems like a function that many would use. By the way, your function would actually return "there world", i only wanted "world". But that's easily fixed, thanks for the help Cory! :)
Nike
+4  A: 

Update2:

If you can asure that given a format of x:y, every x is unique, you can transform the string into an array:

$kvs = explode(',', $string);
$values = array();

foreach($kvs as $kv) {
    list($k, $v) = explode(':', $kv);
    $values[$k] = $v;
}

and then you can get your value via:

  echo $values["20"];

Example: http://codepad.org/qe2i0MUa


Here you go, freshly backed:

function magic_substr($haystack, $start, $end) {
    $index_start = strpos($haystack, $start);
    $index_start = ($index_start === false) ? 0 : $index_start + strlen($start);

    $index_end = strpos($haystack, $end, $index_start); 
    $length = ($index_end === false) ? strlen($end) : $index_end - $index_start;

    return substr($haystack, $index_start, $length);
}

Reference: strpos()

update: Now it works ;)

Example: http://codepad.org/97REFuLm

The built-in function that comes closest to your needs is strstr(), but it only returns the string from a certain substring (to the end).

Felix Kling
+1  A: 

Another freshly baked featuring regex flavor:

function  magic_substr($string,$from,$to) {
   if(preg_match("!".preg_quote($from)."(.*?)".preg_quote($to)."!",$string,$m)) {
           return $m[1];
   }
   return '';
}

Working link

codaddict
+1  A: 
<?php

function magic_substr($string, $search, $delim) {
    preg_match('/'.preg_quote($search, '/').'(.*?)'.preg_quote($delim, '/').'/', $string, $matches);
    return $matches[1];
}

$string = '21:0,19:0,20:5,18:0,17:0,25:0,30:0,23:0,31:0,32:0,33:0';
echo magic_substr($string, '20:', ','); // 5
chigley
A: 
private function magic_substr($haystack, $needle1, $needle2) {
    $start = strpos($haystack, $needle1);
    if ($start === false) {
        return false;
    }
    $start += strlen($needle1);
    $leftTrimmed = substr($haystack, $start);

    return strstr($leftTrimmed, $needle2, true);
}
G Mawr
Returns an empty string for `"Hey! there world!"`...
Felix Kling
Good spot. I've updated the method to fix that.
G Mawr