Jump to content

[SOLVED] return text between 2 strings???????????????????


davidkierz

Recommended Posts

Two options... String functions or regex. Test both to see which one's faster:

 

<?php

$string = "adam baker charlie david edward frank";
$result1 = get_between1($string, "baker", "david");
$result2 = get_between2($string, "baker", "david");

echo $result1."<br>".$result2;

function get_between1($str, $a, $b)
{
$pos_a = strpos($str, $a) + strlen($a);
return trim(substr($str, $pos_a, strpos($str, $b) - $pos_a));
}

function get_between2($str, $a, $b)
{
preg_match("/".preg_quote($a)."(.*?)".preg_quote($b)."/i", $str, $matches);
return trim($matches[1]);
}

?>

 

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.