I have a string with State name and code like this
KT16(Ottershaw)
Now i need to extract text from (). I need to extract Ottershaw. How can i do this with php.
I have a string with State name and code like this
KT16(Ottershaw)
Now i need to extract text from (). I need to extract Ottershaw. How can i do this with php.
should be :
preg_match('/\(([^\)]*)\)/', 'KT16(Ottershaw)', $matches);
echo $matches[1];
Just get the substring between the first opening bracket and the last closing bracket:
$string = "KT16(Ottershaw)";
$strResult = substr($string, stripos($string, "(") +1,strrpos($string, ")") - stripos($string, "(")-1);
$string = "KT16(Otte (rshaw))";$string = "KT16(Otte) test (rshaw)"; ? I'll still give you +1 thought :pThis is a sample code to extract all the text between '[' and ']' and store it 2 separate arrays(ie text inside parentheses in one array and text outside parentheses in another array)
function extract_text($string)
{
$text_outside=array();
$text_inside=array();
$t="";
for($i=0;$i<strlen($string);$i++)
{
if($string[$i]=='[')
{
$text_outside[]=$t;
$t="";
$t1="";
$i++;
while($string[$i]!=']')
{
$t1.=$string[$i];
$i++;
}
$text_inside[] = $t1;
}
else {
if($string[$i]!=']')
$t.=$string[$i];
else {
continue;
}
}
}
if($t!="")
$text_outside[]=$t;
var_dump($text_outside);
echo "\n\n";
var_dump($text_inside);
}
Output: extract_text("hello how are you?"); will produce:
array(1) {
[0]=>
string(18) "hello how are you?"
}
array(0) {
}
extract_text("hello [http://www.google.com/test.mp3] how are you?"); will produce
array(2) {
[0]=>
string(6) "hello "
[1]=>
string(13) " how are you?"
}
array(1) {
[0]=>
string(30) "http://www.google.com/test.mp3"
}