I've got a university project to create a site using a framework given to us by the teacher. I've hosted the site at http://daniel-lucas.com so you can see what's happening.
As you can see the address ends with /?index.
In the configuration file I've just used :
new UrlMapping('^index/$', 'musiconlineapp.listeners.MainListener.handle', 'musiconlineapp/views/MainView.php')
The first parameter is the address to map. The second is the controller for the page and the method to load in the controller (handle). The third is the view to display for the page.
However I'm having trouble displaying categories for the site. I was told to use mapping like this:
new UrlMapping('^category/(?P<cat>\w+)/$', 'musiconlineapp.listeners.CategoryTitleListener.handle', 'musiconlineapp/views/CategoryTitleView.php' )
My problem is that I've not done any regex for a while and can't figure out what address I'm supposed to go to when I click on the link.
I've tried /?category&cat=rock and similar variations but none of them work.
That regex looks like it's supposed to match /?category/rock/ or the like.
^ # beginning of string
category/ # literal 'category/'
(?P<cat> # named matching group (named 'cat')
\w+ # one or more word characters a-zA-Z0-9_
) # end the named matching group
/ # literal '/'
$ # end of string
Related
I have ~300 markdown files held within a single Git repository. I need to change the format of all the internal links within these documents. Internal links are links that do not leave the repository. They look something like this:
Checkout the [new plugin](/developers/tools/plugin/install-the-plugin)
guide if you're stuck. If you know what you're doing head on over to
the [examples section](/developers/examples/plugin-tutorials) and get
your hands dirty.
I need to change all the internal links so that they:
Don't contain /developers/
All the slashes / are converted to dashes -.
The example above should look something like this:
Checkout the [new plugin](tools-plugin-install-the-plugin) guide if
you're stuck. If you know what you're doing head on over to the
[examples section](examples-plugin-tutorials) and get your hands dirty.
One caveat is that I don't want to target images. Images look identical to links, just with an exclamation mark ! at the start:
![Plugin Logo](/developers/tools/plugin/images/logo.png)
I've looked into things and it looks like sed is a way forward in terms of tools. I've managed to build the following regex that captures the links I'm looking for:
\]\(\/developers\/.*\)
This regex doesn't ignore the ![]() image syntax annoyingly. I was able to get PHP to return the locations of each hit on each page, but then I wasn't able to do a find-and-replace on the slashes / within those results.
Any ideas or pointers would be greatly appreciated.
You may do it with a single PHP regex:
$text = preg_replace('~!\[[^][]*]\([^()]*\)(*SKIP)(*F)|(?:\G(?!\A)|(?<=]\()/developers/)([^()/]*)/(?=[^()]*\))~', '$1-', $text)
See the regex demo
Details
!\[[^][]*]\([^()]*\)(*SKIP)(*F) - match !, [, any 0+ chars other than [ and ], then a ](, 0+ chars other than ( and ), ) and then omit the match and go on to search for the next match at the end of the current failed match
| - or
(?:\G(?!\A)|(?<=]\()/developers/) - end of the previous successful match (\G(?!\A)) or (|) a /developers/ string preceded with ](
([^()/]*) - Group 1 ($1): any 0+ chars other than (, ) and /
/ - a / char
(?=[^()]*\)) - ...that is followed with any 0+ chars other than ( and ) and then a ).
it is possible to make an URL alias of a given link of my website? for example I have a page of:
wwww.myste.com/app/subname1
wwww.myste.com/app/subname2
wwww.myste.com/app/subname3
...
and it will be access as
wwww.subname1.myste.com/
wwww.subname2.myste.com/
wwww.subname3.myste.com/
...
is it possible in PHP and htaccess?
well, I am a developing a website system that will create also a subwebsite that will be access as subdomain, just like in weebly.com and wordpress.com.
is it really possible that it can be used in htaccess and PHP, if it is can you give me an example?
my website is hosted in mediatemple and the DNS is on godaddy. I know how o create a subdomain but it needs to go to godaddy and have it own directory in medatemple. But what I really want is that ones a user registered, it will creates it own link like wwww.subname1.myste.com/ instead of wwww.myste.com/app/subname1. is it possible?
If you have enabled mod_rewrite, you could write into <document root>/app/.htaccess:
RewriteEngine on
RewriteRule "^([^/]+)(.*)" "http://www.$1.example.com$2" [R=301,L]
This will redirect /app/subname1/subfolder/file.html to www.subname1.example.com/subfolder/file.html
The rewrite rule is a regular expression matching the path relative to the current directory. The initial ^ forces matching from the start of the subject. The parentheses () declare a subpattern stored in numbered placeholders $1... [abc] means any character of "a", "b" or "c". A ^ as first character within the brackets is a negation, so [^abc] means any character but "a", "b" or "c". The dot matches any character. + and * are multiplier, where + means 1 or more and * means 0 or more occurrences. Multipliers belong to the preceeding expression, not the result. So [abc]+ matches as well to "aaaa" as also to "abca".
The second string is the target.
The third argument [R=301,L] means Redirection 301 (permanently moved); Last rule on match.
So, im trying to select only the content inside the --- traces, which is obviously multiline, but I have to use regular expressions due the fact not all entries will contain this very same amount of meta data.
The example im trying to match using PHP's preg_match function is this:
---
Title: A fresh start to all of us. Right?
Slug: a-fresh-start-to-all-of-us-right
Author: admin
Date: 12/05/2015 16:29
Draft: false
Image: http://placehold.it/400x280
Tags: codesans, install, markdown
---
# A fresh start comes, after all.
As you can see, nothing below the --- traces can be matched.
Im trying to match with this regular expression:
/^(.*)?[^\n]$/gm
but it doesn't seems to work so far. I already tried to tokenize the traces to make them delimiters-like but it also didn't work (this regex: /^(\-{3})?(.*)?(\-{3})?[^\n]$/gm).
Any guidance, please?
You need to use DOTALL flag i.e. s for this:
/(\A|\R)-{3}\R(.+?)-{3}(\R|\z)/s
btw there is no g flag in PHP.
RegEx Demo
Could use something like this (?m)^---.*\s+([\S\s]*?)^---
This way uses the normal dot.
(?m)
^ --- .* \s+
( [\S\s]*? ) # (1)
^ ---
I need to match "name" only after "listing", but of course those words could be any url directory or page.
mydomain.com/listing/name
so the only thing I can "REGuest" (request) is to be some parent directory there.
In other words, I want to match the "position" i.e. whatever comes 2nd after the domain.
I'm trying something like
(?<=mydomain\.com/[^/\?&]+/)[^/\?&]+(?:/)?
But the character set won't work inside the positive lookbehind, at least it's setup to match only ONE character. As soon as I try to match other than one (e.g. modify it with +, ? or *) it just stops working.
I'm obviously missing the positive lookbehind syntax and it seems not intended for what I'm trying.
How can I match that 2nd level filename?
Thanks.
Regular-expressions.info states that
The bad news is that most regex flavors do not allow you to use just
any regex inside a lookbehind, because they cannot apply a regular
expression backwards. Therefore, the regular expression engine needs
to be able to figure out how many steps to step back before checking
the lookbehind...
(Read further, they even mention Perl, Python and Java.)
I think the quantifier might be the problem. I found this on stackoverflow and briefly flew over it.
Wouldn't it be possible to just match the whole path, and use a group for the second level filename:
mydomain\.com\/[^\/\?&]+\/([^\/\?&]+)(?:\/)?
(note: I had to escape the / for my tests...)
The result of this would be something like:
Array
(
[0] => mydomain.com/listing/name
[1] => name
)
Now, because I don't know the context of your problem, I just assumed you would be able to postprocess the results and get the group 1 (index 1) from the result. If not, I unfortunately don't know...
im trying to rewrite a url like
page.php?sort=66&search=s&category=2,3,4&archive=june&page=3
to
page-sort-1-search(s)-category-1,2,3-archive(june)-page3
but the thing is each of this subexpressions my or may not be in the url every time that this page is called so i had to put each one in the "( )?" so the regex works with or without them
^page
(-sort-([0-9]*))?
(-search\(([a-z]*)\))?
(-category-([0-9][,]?*))?
.............
you get the idea
now the problem is mode rewrite is considering each one of this subexpression in the parenthesis as an actual variable
(-sort-([0-9]*))? this is how mode rewrite interpret this => $1 = -sort-66 , $2 = 66
so for each subexpression i got 2 capture-group and that's more then 10 for a link with 5-6 variable
and there is a 9 match limit in mode rewrite
is there a replacement for "()?"
I have to admit, I'm a bit confused by some parts of your question, so I apologize in advance if my answer is totally off-base . . . but it sounds like what you need is a non-capturing group. This:
(...)
matches ... and creates a capture-group (what you're calling a "variable"); this:
(?:...)
just matches ..., without creating a capture-group. So it's useful if all you want to do is group a subexpression, without saving all of its contents to be used as $1.