如何使用php分解单词


How to explode word using php

如何在php中取词或基于luas Bangunan:xxx
例如,我有字符串

$string ="                 Kondisi Properti            : Bagus                    Dilengkapi Perabotan      : Unfurnished                    Sertifikat    : Lainnya                        Daya Listrik          : 2200 Watt                           Kamar Tidur         : 3/1                   Kamar Mandi      : 2/1                  **Luas Bangunan      : 92** m&sup2;                     Luas Tanah     : 126 m&sup2;                  Jumlah Lantai      : -<br>                 Kondisi Properti            : Bagus Sekali                       Dilengkapi Perabotan      : Unfurnished                    Sertifikat    : SHM - Sertifikat Hak Milik                         Daya Listrik          : 6600 Watt                       Saluran Telepon          : 1                        Garasi          : 3                             Kamar Tidur         : 4/1                   Kamar Mandi      : 3/1                  **Luas Bangunan      : 300** m&sup2;                     Luas Tanah     : 228 m&sup2;                 Jumlah Lantai      : 2.5                          ";

例如,我想将每个"Luas bangunan:xxx"

嗯,这是一个特定的需求。在你的位置,我会用空格$array=explose(",$string)分解字符串,用foreach探索数组($array as$key=>$value),并测试每个值是否与Luas和strpos匹配($value,'Luas')。因此,当它匹配"Luas"时,您应该使用if条件进行测试,该条件接受数组中接下来的3个索引,然后将这4个字连接并保存在这个分离的数组中。

它是这样的:

$array = explode(' ',$string);
$match = 'Luas';
$matchesArray = array();
foreach($array as $key => $value){
    //this var must word just like a temporary helper
    $target = '';
    //test each word
    if(strpos($value,$match) === true){
        //lets take the next word, 2 dots and the other word
        for($counter = 0;$counter <= 3;$counter++){
            //here we may construct our target phrase
            $nextWordsIndex = $key+$counter;
            $target.= $array[$nextWordsIndex].' ';
        }
        //but after the loop? Yes! we should take our target ready with 3 words
        $matchesArray[] = $target;
    }
}
//Now you should have a matchesArray with your wanted Information in each index.

用->preg_match_allpreg_split尝试这些解决方案,我把它们都放了,因为我没有完全理解你想要的:)

<?php
$string =" Kondisi Properti : Bagus Dilengkapi Perabotan : Unfurnished Sertifikat : Lainnya Daya Listrik : 2200 Watt Kamar Tidur : 3/1 Kamar Mandi : 2/1 Luas Bangunan : 92 m² Luas Tanah : 126 m² Jumlah Lantai : -
Kondisi Properti : Bagus Sekali Dilengkapi Perabotan : Unfurnished Sertifikat : SHM - Sertifikat Hak Milik Daya Listrik : 6600 Watt Saluran Telepon : 1 Garasi : 3 Kamar Tidur : 4/1 Kamar Mandi : 3/1 Luas Bangunan : 300 m² Luas Tanah : 228 m² Jumlah Lantai : 2.5 ";
preg_match_all("/Luas Bangunan : [0-9]+/",$string,$out);
echo "<pre>";
print_r($out);

$keywords = preg_split("/Luas Bangunan : [0-9]+/" ,$string );
echo "<pre>";
print_r($keywords);