2015-12-14 14:05
浏览 34

用于jQuery UI自动完成的简单PHP脚本(不含MySQL)

I have the following HTML with jQuery:

<!DOCTYPE html>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../scripts/jquery-2.1.4.min.js"></script>
    <link href="../scripts/ui/jquery-ui.min.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="../scripts/ui/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(function() {
                source: "autocomplete.php"
    <div class="ui-widget">
        <label for="acInput">Flower Name: </label><input type="text" name="acInput" id="acInput">

and I want to create a PHP file (autocomplete.php which would be in the same directory) that will search through array and return response:

I've tried the following way:

$flowers = ["Aster", "Daffodil", "Rose","Peony", "Primula", "Snowdrop", "Poppy", "Primrose", "Petuna", "Pansy"];
$matches = [];
if (isset($_GET["term"])) {  
    $term = trim($_GET["term"]);  // do I need to remove/strip tags or escape it and what is the best way?
    if (!empty($term)) {
        $pattern = '/^'+$term+'/';
        foreach ($flowers as $cvet) {
            preg_match($pattern, $cvet, $matches);
        echo json_encode($matches);

But it doesn't work. I assume that the problem has to do with $pattern or preg_match, since this is the first time I use regular expression with PHP?

EDIT: What I want is to check:

1. Are there values in the array $flowers that are equal to $term OR start with $term;

2. Are there values in the array $flowers containing $term.

Return all of these $flowers array values (elements) to jQuery autocomplete and display them as suggestions. Thanks!

图片转代码服务由CSDN问答提供 功能建议


 &lt;  !DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; meta charset =“UTF-8”&gt; 
&lt; title&gt;测试&lt; / title&gt; 
&lt; script type =“text / javascript  “src =”../ scripts / jquery-2.1.4.min.js“&gt;&lt; / script&gt; 
&lt; link href =”../ scripts / ui / jquery-ui.min.css“rel  =“stylesheet”type =“text / css”&gt; 
&lt; script type =“text / javascript”src =“../ scripts / ui / jquery-ui.min.js”&gt;&lt; / script&gt;  
&lt; script type =“text / javascript”&gt; 
&lt; / script&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; div class =“ui-widget”&gt; 
&lt; label for =“acInput”&gt;花名:&lt;  ; / label&gt;&lt; input type =“text”name =“acInput”id =“acInput”&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
  <  / pre> 

我想创建一个PHP文件(autocomp lete.php将在同一目录中)搜索数组并返回响应:


 $ flowers = [“Aster”,“Daffodil”,“Rose”,“Peony”,“Primula”,“Snowdrop”,“Poppy”,“Primrose”,“Petuna”,“  Pansy“]; 
 $ matches = []; 
if(isset($ _ GET [”term“])){
 $ term = trim($ _ GET [”term“]);  //我需要删除/去除标签还是逃避它以及最好的方法是什么?
 if(!empty($ term)){
 $ pattern ='/ ^'+ $ term +'/'; 
  foreach($ flowers as $ cvet){
 preg_match($ pattern,$ cvet,$ matches); 
 echo json_encode($ matches); 

但它不起作用。 我认为问题与 $ pattern preg_match 有关,因为这是我第一次使用PHP的正则表达式? < p> 编辑:我想要检查:

1。数组$ flowers中的值是否等于 $ term或以$ term开头;

2。数组$ flowers中是否包含$ term的值。

将所有这些$ flowers数组值(元素)返回到jQuery自动完成并将其显示为建议。 谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drv16821 2015-12-14 14:10

    I would rather give you a much simplified version, without using regex:

      $flowers = ["Aster", "Daffodil", "Rose","Peony", "Primula", "Snowdrop", "Poppy", "Primrose", "Petuna", "Pansy"];
      // If the `term` is set.
      if (isset($_GET["term"])) {
        $flowers = array_filter($flowers, "filter_out");
      // Callback function for the array to filter only those values contain the term.
      function filter_out ($var) {
        return strpos($var, $_GET["term"]) !== false;
      // encode and send it back.
      echo json_encode($flowers);


    Request: autocomplete.php


    Request: autocomplete.php?term=p

    点赞 打赏 评论
  • dongtan3306 2015-12-14 14:23
    $flowers = ["aster", "daffodil", "rose","peony", "primula", "snowdrop", "poppy", "primrose", "petuna", "pansy"];
    $matches = [];
    if (isset($_GET["term"]) && !empty($_GET["term"])) {  
        $term = trim( strip_tags($_GET["term"]) );  
        if (!empty($term)) {
            $pattern = '/^'. strtolower($term) .'/ui';
            foreach ($flowers as $cvet) {
                preg_match($pattern, $cvet, $m);
                if(isset($m[0])) $matches[] = $m[0]; // or ucfirst($m[0]) (if you want)
            echo json_encode($matches);
    点赞 打赏 评论

相关推荐 更多相似问题