0

The user will input on a text field something like this:

$list = "hello, internet, connection, wireless"

I'm getting them out with the following:

$tags = explode(',', $list);

The problem is that before I do that, I need to validate whether the format of the string captured in the $_POST actually has that format. Any ideas how can I do that? I'm not an experienced PHP programmer, I usually just work on front end development.

The user can input more than 4 tags, it's not always going to be 4, he can put 5 or 6 with a maximum of 7, the output I want is to have on an array, all the words in the string, the background for this is that I'm making a simple helpdesk, where users can ask a question, and I will return all the results according to the found tags in the question the user has input.

So I really need to make sure that when the ADMIN of this helpdesk is typing the tags, they're on that format:

$data = "internet, connection, wireless, internet-explorer";
4

1 回答 1

0

我取决于内容是否每次 4 元素。

<?php

    $list = "hello, internet, connection, wireless";
    echo substr_count($list,', '); // return 3 commas

?>                                                                                      

或者你计算元素:

<?php
     $list = "hello, internet, connection, wireless";
     $result = explode(', ',$list);
     if(count($result) ==4 ){
         echo "right format";
     }
?>

编辑:

当您不需要空格时:

<?php
     $list = "hello, internet, connection, wireless";
     $result = array_map('trim',explode(",",$list)); 
     if(count($result) ==4 ){
         echo "right format";
     }
?>
于 2013-10-27T16:19:15.047 回答