php


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.
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).
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. You can use file_exists() to check if a file exists (or not !).
I would use the following expression: #(\S{1,}). This expression looks for a #, then starts capturing any non white-space character (/S), from once to infinite times ({1,}).
<?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 #. 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. For instance, if the hashtag folder has not been created yet, it will create that too.
After doing some research, since "#" can be found without using regex, it's much more efficient to use strpos() and explode(). preg_match() vs strpos() speed is shown below.
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. I would be interested in seeing the two answers code put head to head as it would be quite interesting to know for sure.

Related Links

How to draw a name in a database [duplicate]
PHP: Generating category/subcategory UL list from array?
wikipedia servers gzip content
How to escape ' in setParameter
onclick submit check php
php: how to shoow session variable on every php page [duplicate]
Multilingual site: switching languages
jQuery hide() flicker
Cascading dropdown with jQuery
Regex to match alphanumeric characters, underscore, periods and dash, allowing dot and dash only in the middle
Setting Facebook PHP SDK access_token with setAccessToken()
How do I modify the path used by exec in php
Broken link check while initiating cron-multi
How to format `23/05/2012` into insertable value in DATETIME column?
will users see post on their wall,if a post is made on app page
Wordpress Loop passing variables

Categories

HOME
visual-studio
svn
livecode
enums
tcsh
plaid
openlayers-3
android-emulator
bing-search
google-webmaster-tools
sympy
kurento
cocotb
fireloop
getopenfilename
graphdb
documentum
angular4
smartgwt
google-sites
maven-plugin
salt-cloud
scheduled-tasks
unity-container
tableview
altium-designer
quantitative-finance
perfino
cell
quartz.net
managed-c++
points
ninject
tsung
commonsware-cwac
reporting
john-the-ripper
custom-keyboard
bean-validation
pdfminer
numerics
redgate
macports
newtons-method
secure-coding
vungle-ads
permission-denied
apache-toree
vesta
destroy
sharpdx
karabiner
webvtt
mura
nslayoutconstraint
mobile-angular-ui
mechanicalturk
d3v4
finder
petsc
livecycle
tsqlt
openseadragon
piping
groovyfx
eclemma
gnucash
apache-pig-grunt
maximize
distributed-cache
mercurial-extension
ioctl
pack
android-mapview
smart-tv
dsa
screen-orientation
espresso
fantom
uitest
actionpack
scala-macros
mojo
famo.us
adwords-apiv201402
relationships
fileshare
zend-search-lucene
smips
illuminate-container
jquery-transit
void-pointers
arel
semicolon
proc-open
stretch
kaazing
exponent
server-variables
office-automation
winsxs
kohana-auth
webresponse
principles
fgetc
aspbutton
getimagesize
ppc
spring-modules
subtext
acceleration

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App