简体   繁体   English

根据发布的主题标签创建文件夹

[英]Creating folders based on posted hashtags PHP

So this is my first time posting a question on this website and as a programmer I'm ashamed I didn't create an account sooner, this website has gotten me through a lot lol, anyways enough about that, I need some help. 因此,这是我第一次在该网站上发布问题,作为一名程序员,我感到as愧,因为我没有尽快创建一个帐户,这个网站已经让我度过了很多大笑,无论如何,我需要一些帮助。

I am creating a system where users can post their own little statuses, update their status, share, etc. I need help creating folders for #hashtags based on the hashtag the user has in his posted status. 我正在创建一个系统,用户可以在其中发布自己的小状态,更新其状态,共享等。我需要帮助,请根据用户已发布状态中的标签为#标签创建文件夹。

So an example would be: " Hello world #firstpost #helloworld ", the code would have to exclude everything in that sentence except for any and all hashtags, and it would then create the folders for all the hashtags that are in the users post, in this case it's going to create the folders firstpost and helloworld (if they don't already exist). 因此,示例为:“ Hello world #firstpost #helloworld ”,代码将必须排除该句子中除所有标签和所有标签以外的所有内容,然后它将为用户发布中的所有标签创建文件夹,在这种情况下,将创建文件夹firstposthelloworld (如果它们尚不存在)。

Can I create the folders using a modified version of this or at least something better/similar/related? 我是否可以使用此版本或至少更好/类似/相关的版本创建文件夹?

$foldername = $_POST["status_posting"];
mkdir('../hashtag/' . $foldername, 0755);
for ($i = 0; $i < 1; $i++) {
    mkdir('../users/' . $foldername, 0755);
}

Thank you for your guys help in advance. 谢谢您的帮助。

You can use a regular expression to capture the hashtags, then use mkdir() to create the directories. 您可以使用正则表达式捕获主题标签,然后使用mkdir()创建目录。 You can use file_exists() to check if a file exists (or not ! ). 您可以使用file_exists()来检查文件是否存在(或不存在! )。

I would use the following expression: #(\\S{1,}) . 我将使用以下表达式: #(\\S{1,}) This expression looks for a # , then starts capturing any non white-space character ( /S ), from once to infinite times ( {1,} ). 该表达式查找# ,然后开始捕获一次到无限次数( {1,} )的任何非空格字符( /S )。

<?php
    $input = "Hello world #firstpost #helloworld";

    preg_match_all("/#(\S{1,})/", $input, $matches);

    foreach($matches[1] as $match){
        if (!file_exists('../hashtag/' . $match)) {
            mkdir('../hashtag/' . $match, 0755, true);
        }
    }

I'd php explode() the message, check each item if it contains a # , if so mkdir the item's name minus the # . 我会用php explode()消息,检查每个项目是否包含# ,如果这样,则该项目的名称减去# Possibly not the most efficient way to do it. 可能不是最有效的方法。

$foldername = explode(" ", $_POST["status_posting"]);
foreach($foldername as $string){
  if(strpos($string, '#') !== FALSE){
    //Make dir!
    mkdir('../hashtag/' . ltrim($string, "#"), 0755, true);
  }
}

The true at the end of mkdir allows for recursive file path creation. mkdir末尾的true允许创建递归文件路径。 For instance, if the hashtag folder has not been created yet, it will create that too. 例如,如果还没有创建hashtag文件夹,它也会创建它。

After doing some research, since "#" can be found without using regex, it's much more efficient to use strpos() and explode() . 经过研究后,由于无需使用正则表达式即可找到“#”,因此使用strpos() and explode()效率更高。 preg_match() vs strpos() speed is shown below. preg_match() vs strpos()速度如下所示。

no. strings  1        2       3         10      100     1000    10000
strpos()     0.01 ms  0.02 ms 0.04 ms   0.2 ms  0.9 ms  2.6 ms  25.6 ms
preg_match() 0.2 ms   0.2 ms  0.3 ms    0.47 ms 0.95 ms 7.4 ms  72.2 ms

Though you do have to factor in explode() time, the php forum states that explode() should be used when regex power is not required, for reasons we can see above. 尽管您必须考虑explode()时间,但php论坛指出,不需要正则表达式电源时应使用explode(),出于上述原因。 I would be interested in seeing the two answers code put head to head as it would be quite interesting to know for sure. 我很想看到两个答案代码并列,因为肯定会很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM