我目前正在为我的网站开发一个非常简单的搜索功能。用户键入他们的搜索查询,然后他们被带到显示结果的页面。就我而言,用户正在搜索其他用户。我的问题是,我如何将一个字符串分成两个变量?用PHP分隔字符串
实例查询:
马特·莫里斯
我想我的PHP到空间发生串分开。我怎样才能做到这一点?
<html>
<form method="get" action="results.php">
<input type="text" name="search"/>
</form>
</html>
<?php
// code to explode the string into $fname and $lname
if there is a space present
$fname= Matt
$lname= Morris
if (isset($fname) && isset($lname)) {
$search_query= "SELECT * FROM users WHERE fname LIKE %'".$fname."'% AND lname LIKE
%'".$lname."'%";
// code to run the query and display results
}
else if (isset($fname)) {
$search_query= "SELECT * FROM users WHERE fname LIKE %'".$fname."'%";
// code to run the query and display results
}
else if (isset($lname)) {
$search_query= "SELECT * FROM users WHERE lname LIKE %'".$lname."'%";
// code to run the query and display results
}
?>
感谢
你必须警惕爆炸的字符串,并期望获得fname/lname,有些人的姓氏中有空格(有时是第一个)。 – Brian
作为一个了解你没有清理任何东西的精明用户,我只需输入'Matt%Moris'作为搜索词。 – mario
输入是做什么的?我用mysql_real_escape_string()是好还是过时? – user1759682