• support@answerspoint.com

Extract a substring between two characters in a string PHP

695
$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 3 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 3 years ago
  • Gul Hafiz

Your Answer

    Facebook Share        
       
  • asked 3 years ago
  • viewed 695 times
  • active 3 years ago

Hot Questions