John Marshall John Marshall - 7 months ago 11
PHP Question

split a variable with brackets php mysql

I know how to do this once the data is already in my MySQL db but what I'm trying to do is
I have a variable called $host that will always be like RockN_Roll_lady (MisFit_kay1) what I am looking for a point in the right direction is how to make my variable split so that $host field will be RockN_Roll_lady and $hostplayername will be MisFit_kay1

I know using MySQL it can be done once its inserted using

Update tourne_report set Host= SUBSTRING_INDEX( Host , '(', 1 ) ,Hostplayername= SUBSTRING_INDEX(SUBSTRING_INDEX( Host , '(', 2 ),'(',-1) ;


but I'm looking for a solution to take the $host variable and make it into 2 variables before the data gets entered into my database

Answer

Try this

<?php

$str            = "RockN_Roll_lady (MisFit_kay1)";
$arrChunks      = preg_split("#\(#", $str);
$host           = trim($arrChunks[0]);
$hostplayername = trim($arrChunks[1], ")");

var_dump($host);
var_dump($hostplayername);
Comments