2

Ok to be clear ill give an example

$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name

as it says, it prints what before '@' using true, and blank to print what follows @

im looking for a function to print @ itself by giving it 2 strings and grab what between them

like this

  $string= 'someXthing';
  $tograb = phpfunction("some","thing");
  echo $tograb; // should be printing X

^ this doesnt work, im just writing it to explain

Lili Abedinpour
  • 159
  • 1
  • 4
  • 12

3 Answers3

1

I dont know of a native function that does that but you can use regular expression

$string= 'someXthing';
preg_match("/some(.*)thing/",$string,$matches);
var_dump($matches[1]);

Read More about preg_match

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • that kinda worked but how to make it to only print X without some thing – Lili Abedinpour Apr 13 '12 at 17:45
  • @LiliAbedinpour the variable $matches is an array, so you can `echo $matches[1]` to see the matched string. – Ibu Apr 13 '12 at 17:49
  • ohhh sorry yes got it now, thanks im fine with this but i think there should be a function for that, i guess it will mostly be needed in future ! thanks anyway – Lili Abedinpour Apr 13 '12 at 17:54
1

From internet

function GetBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}

Original code

magallanes
  • 6,583
  • 4
  • 54
  • 55
0

For the example you said, you can use strpos to print X like this:

$string = 'someXthing';

$start = strpos($string, "some") + strlen("some");
$end = strpos($string, "thing", $start);
$tograb = substr($string, $start, $end - $start);

echo $tograb;

and X will be printed.

Maryam Homayouni
  • 905
  • 9
  • 16