I want to make this words/phrases/sentence split into three parts. fld_example is where the words/phrases/sentence are stored.

Stack Over Flow Abcpqr (UR)

Fluency in English Conversation Defklmno (1WIR)

English Proficiency GHI (2WIR)

Testing ADG (3WIR)

the output for Stack Over Flow Abcpqr (UR) should be

[0] Stack Over Flow

[1] Abcpqr

[2] (UR)

and for Fluency in English Conversation Defklmno (1WIR)

[0] Stack in Over Flow

[1] Defklmno

[2] (1WIR)

and for English Proficiency GHI (2WIR)

[0] Over Flow

[1] GHI

[2] (2WIR)

and for Testing ADG (3WIR)

[0] Testing

[1] ADG

[2] (3WIR)

I used this code but it is only good for Testing (3WIR)

                                      <?php
                                      $Original = $row['fld_example'];                                  
                                      $OriginalExplode = explode(' ', $Original);
                                      ?>

<input name="example0" id="example0" value="<?php echo $OriginalExplode[0]; ?>" type="text" autocomplete="off" required>

<input name="example1" id="example1" value="<?php echo $OriginalExplode[1]; ?>" type="text" autocomplete="off" required>

2 Answers

1
Rakesh Kumar Jakhar On Best Solutions

You can approach this using explode and str_replace

$string = "Testing (3WIR)";
$stringToArray = explode(":",str_replace("(",":(",$string));
echo '<pre>';
print_r($stringToArray);

Edited question answer:-

$subject = "Fluency in English Conversation Defklmno (1WIR)";
$toArray = explode(' ',$subject);
if(count($toArray) > 2){
  $first       = implode(" ",array_slice($toArray, 0,count($toArray)-2));
  $second      = $toArray[count($toArray)-2];
  $third       = $toArray[count($toArray)-1];
  $result      = array_values(array_filter([$first, $second, $third]));
}else{
  $result = array_values(array_filter(explode(":",str_replace("(",":(",$subject))));
}

DEMO HERE

1
KIKO Software On

I am not a fan of regular expressions, but this one seems to work very fine:

Regex to split a string only by the last whitespace character

So the PHP code would be:

function splitAtLastWord($sentence)
{
    return preg_split("/\s+(?=\S*+$)/", $sentence);
}

$sentence = "Fluency in English Conversation Defklmno (1WIR)";  

list($begin, $end)    = splitAtLastWord($sentence);
list($first, $middle) = splitAtLastWord($begin);
$result = [$first, $middle, $end]; 

echo "<pre>" . print_r($result, TRUE) . "</pre>";

The output is:

Array
(
    [0] => Fluency in English Conversation
    [1] => Defklmno
    [2] => (1WIR)
)

You can also write the same function without a regular expression:

function splitAtLastWord($sentence)
{
    $words = explode(" ", $sentence);
    $last  = array_pop($words);
    return [implode(" ", $words), $last];
}

Which is, to be honest, a better way of doing this.

This is a computationally more efficient way to do it:

function splitAtLastWord($sentence)
{
   $lastSpacePos = strrpos($sentence, " ");
   return [substr($sentence, 0, $lastSpacePos), substr($sentence, $lastSpacePos + 1)]; 
}

It looks a bit less nice but it is faster.

Anyway, defining a separate function like this is useful, you can reuse it in other places.