0

I have a string with underscores(_). What I want is to get the string value after the first underscore and considered as the first string value and the second string value will be the whole string after the underscore of the first string value using php.

Example:

$string = "Makes_me_wonder";

Result I want:

$str1 = "me";
$str2 = "wonder";

Another variable I am having:

$string = "I_wont_gohome_withoutyou";

Result should be:

$str1 = "wont";
$str2 = "gohome_withoutyou";

Another one:

$string = 'Never_gonna_leave_this_bed";

Output i want:-

$str1 = "gonna_leave"; 
$str2 = "this_bed";

Please help me. Thanks.

4
  • 1
    oh sorry! I will edit it. Thanks!! Commented Jun 22, 2016 at 5:19
  • 1
    What if it is only Makes_me? Will your string will have always more than 2 underscores? Commented Jun 22, 2016 at 5:21
  • @Thamilan no it won't happen. Commented Jun 22, 2016 at 5:41
  • 1
    @Anant yours is a good answer. How can I mark it as correct answer? Commented Jun 22, 2016 at 5:42

4 Answers 4

2

You can use explode with 3rd parameter - limit:

DEMO

$string = "I_wont_gohome_withoutyou";

$arr = explode("_",$string,3);

$str1 = $arr[1];   //wont
$str2 = $arr[2];   //gohome_withoutyou

Provided that you have two or more _ in a word strictly. If it is so, there needs a work around too.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh! I forgot. There is another case that will surely happen. I edited my question.
2
function explode($string)
{
    $delimiter = '_';
    return explode($delimiter, explode($delimiter, $string, 2)[1], 2);
}

$string = "Makes_me_wonder";
$strings = explode($string);
echo $strings[0]; //me
echo $strings[1]; //wonder

$string = "I_wont_gohome_withoutyou";
$strings = explode($string);
echo $strings[0]; //wont
echo $strings[1]; //gohome_withoutyou

3 Comments

your solution doesn't covers op's second case
Oh! I forgot. There is another case that will surely happen. I edited my question.
Oh! I forgot. There is another case that will surely happen. I edited my question.
1

I think your solution is like this:-

<?php
function getfinal_result($string){
    $final_data = explode('_',$string,2)[1]; // explode with first occurrence of _ and leave first word
    if(substr_count($final_data,'_')>2){ // now check  _ remains  is greater that 2
        $first_data = substr($final_data , 0, strpos($final_data , '_', strpos($final_data , '_')+1)); // find the string comes after second _
        $second_data = str_replace($first_data.'_','',$final_data); // get the string before the second _
        $last_data = Array($first_data,$second_data); // assign them to final data
    }else{
        $last_data = explode('_',$final_data,2); // directly explode with first occurance of _
    }
    return $last_data; // return final data

}
$first_data = getfinal_result('Makes_me_wonder');
$second_data = getfinal_result('I_wont_gohome_withoutyou');
$third_data = getfinal_result('Never_gonna_leave_this_bed');

echo "<pre/>";print_r($first_data);
echo "<pre/>";print_r($second_data);
echo "<pre/>";print_r($third_data);
?>

Output:- https://eval.in/593240

2 Comments

Oh! I forgot. There is another case that will surely happen. I edited my question.
@PHPNoob i updated my answer. Please check and if correct then mark and up-vote the answer. thanks.
1

There are multiple methods but here's one of them.

$pos1 = strpos($string, '_');
$pos2 = strpos($string, '_', $pos1 + 1);
$str1 = substr($string, $pos1 + 1, $pos2 - $pos1 - 1);
$str2 = substr($string, $pos2 + 1);

This assumes that there are at least 2 underscores in the string.

3 Comments

Oh! I forgot. There is another case that will surely happen. I edited my question.
@PHPNoob That second case doesn't make sense. You sure it's correct?
yes I am sure it's correct it sometimes appear a string like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.