cihan100
cihan100

Reputation: 21

How can I split multiple joined words in PHP

Example string: "outofthebox"

I want to get output like this: Array ( [0] => out [1] => of [2] => the [3] => box )

What do I get right now: Array ( [0] => out [1] => oft [2] => heb [3] => ox )

I don't know how it's possible. I need that logic, how can I get more meaningful results.

I'm building it on PHP based on this https://stackoverflow.com/a/481773/17035224 Python answer. But I'm not good at Python. In this python script it's returning results like exactly what I want. Another script in python called 'wordninja' is working great too.

My PHP script:

<?php
$db = new PDO("mysql:host=localhost;dbname=strings", "root", "");
$text = "outofthebox";

$finish = false;
$words = [];
$find = false;
$start = -1;
$added = false;
$comp = [];
for($i = 0; $i<strlen($text); $i++) {
    if(count($words) > 0) {
        $last = max($words);
        $comp[] = $last;
        $words = [];
        $start = strlen(implode("", $comp));
        if($added === true) {
            $added = false;
        }else {
            $start++;
        }
    }else {
        $start++;
    }

    $part = "";
    for($j = $start; $j<strlen($text); $j++) {
        $part .= $text[$j];
        echo $part."<br>";
        $check = checkWord($part);
        if($check === true) {
            $added = true;
            $words[] = $part;
        }
    }
}

print_r($comp);

function checkWord($text) {
    global $db;
    $check = $db->query("select * from strings where string='".$text."'")->fetch(PDO::FETCH_ASSOC);
    if(isset($check["id"]) == true) {
        return true;
    }
    return false;
}

Other difference is as you see I'm using mysql database for dictionary instead of txt.

Upvotes: 2

Views: 131

Answers (1)

Mihai C
Mihai C

Reputation: 33

If you change the checkWord function to this :

function checkWord($text) {
   
    $arr = [
        'out',
        'of',
        'the',
        'box',
    ];

    if(in_array($text, $arr)) {
        return true;
    }
    
    return false;
}

You will see that the result will be :

Array ( [0] => out [1] => of [2] => the [3] => box )

So my guess is that you have false-positives coming from your query, check that and you'll solve the problem.

Upvotes: 1

Related Questions