• support@answerspoint.com

Extract a substring between two characters in a string PHP

2910
$String = [modid=256];

$First = "=";
$Second = "]";

$id = substr($string, $First, $Second);

Thus $id would be 256

Any help would be appreciated :)

2Answer


0

use this code

$input = "[modid=256]";
preg_match('~=(.*?)]~', $input, $output);
echo $output[1]; // 256

working example http://codepad.viper-7.com/0eD2ns

  • answered 8 years ago
  • G John

0
<?php

$str = "[modid=256]";
$from = "=";
$to = "]";

echo getStringBetween($str,$from,$to);

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

?>
  • answered 8 years ago
  • Gul Hafiz

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 2910 times
  • active 8 years ago

Best Rated Questions