如何打破不同部分中地址的值?在不同部分中断字符串
例如:Jl。作品编号181 RT 006 RW 011出品。 Sukabungah Kec。苏卡加迪万隆市
是:
$address = "Jln. Suka cita No 1, RT 001 RW 002 Kota Bandung";
到:
$jln = "suka cita no 1 ";
$rt = "001";
$rw = "002";
$kota = "bandung";
如何打破不同部分中地址的值?在不同部分中断字符串
例如:Jl。作品编号181 RT 006 RW 011出品。 Sukabungah Kec。苏卡加迪万隆市
是:
$address = "Jln. Suka cita No 1, RT 001 RW 002 Kota Bandung";
到:
$jln = "suka cita no 1 ";
$rt = "001";
$rw = "002";
$kota = "bandung";
正则表达式是用于拉出特定结构的字符串的部分工具......但前提是你可以依靠它预期的格式之中。
$address = "Jln. Suka cita No 1, RT 001 RW 002 Kota Bandung";
// Match everything from the start to the first comma
preg_match('/^([^,]+),/', $address, $matches);
$jln = $matches[1];
// Match a row of digits appearing atter " RT "
preg_match('/ RT (\d+) /', $address, $matches);
$rt = $matches[1];
// Match a row of digits appearing after " RW "
preg_match('/ RW (\d+) /', $address, $matches);
$rw = $matches[1];
// Match everything from the string "Kota " to the end of line
preg_match('/Kota (.+)$/', $address, $matches);
$kota = $matches[1];
代码没有经过测试,因为我写的我的电话这个答案,但是这将是非常类似的措施。
华,非常感谢它的工作 –
如果你构建以适当的方式在数据库中的数据会更容易实现这一目标。尝试这样的事情。
首先,你需要将数据存储在你需要它的结构数据库,您将需要它来创建与行的表有点像这样:
jln
rt
rw
kota
的SQL查询来实现这一目标看起来像下面:
CREATE TABLE IF NOT EXISTS `test` (
`jln` text NOT NULL,
`rt` text NOT NULL,
`rw` text NOT NULL,
`kota` text NOT NULL
);
INSERT INTO `test` (`jln`, `rt`, `rw`, `kota`) VALUES
('suka cita no 1', '001', '002', 'bandung');
查询数据库拉出值
// Open database
$m_db = new mysqli('your_hostname', 'your_username'], 'your_password', 'your_db_name']);
// Check for errors
if ($m_db->connect_errno > 0) {
trigger_error('Unable to connect to database [' . $m_db->connect_error .
']', E_USER_ERROR);
}
// Query the database
$result = $db->query("SELECT * FROM test");
// Check that there is a valid result
$data = array();
if ($result) {
// Put all of the results into an array
while ($result_row = $result->fetch_assoc()) {
$data[] = $result_row;
}
// Free the database handle
$result->free();
}
// Example code to display output for debugging
foreach ($data AS $values) {
print_r($values);
}
这将会把你的价值观到一个数组如下:
$values[0]['jln'] = 'suka cita no 1'
$values[0]['rt'] = '001'
$values[0]['rw'] = '002'
$values[0]['kota'] = 'bandung'
如果有“while”为什么我们再次使用“foreach”? 我从w3school读取的只是清晰的使用 –
foreach部分只是一个示例,向您显示代码将输出的数据。 – MikeyBunny
你的问题是什么? –
打破可变 从:$ ADRESS = “惹须贺CITA否1,RT 001 RW 002哥打万隆。” 到: $ JLN =苏卡CITA无1 $ RT = 001 $ RW = 002 $科塔= bandung –