Regular expression for a redirect - php

I'm trying to write a regular expression for a redirect and not having any luck. In this example, an old URL might exist like this:
example.com/about-us/Default.asp
example.com/the-team/Default.asp
Which I want to redirect to:
example.com/about-us/
example.com/the-team/
I've come up with this:
/(\d*)/Default.asp
Which doesn't work...
I've also tried this:
/(\d*)/Default\.asp
As I thought there might be a problem with not having an escape char for the '.', still no luck. Can anyone see what I'm doing wrong?

Got it working thanks to what minitech pointed out:
/(.*)/Default.asp$
worked a treat! Thanks

Since you only need to remove the "Default.asp", you only have to search for that. The regex would look something like this
/Default\.asp/
The dot being escaped since the dot is a special character.
If you're using php, you can do a simple preg_replace
preg_replace('/Default\.asp/', '', 'example.com/about-us/Default.asp');

Related

PHP preg_match not giving result

I am having some problems trying to datamine this site, maybe you can help?
$content = file_get_contents('http://store.steampowered.com/app/8190/');
$regexp='#(.*?)#';
preg_match($regexp,$content,$string1);
print_r($string1);
This code doesn't seem to work, maybe it seems obvious to you? Thanks :)
You have not escaped the hyperlink tag, try this:
$regexp='#<a\s+href\s+=\s+"http://store\.steampowered\.com/search/\?category2=2"\s+class\s+=\s+"name">(.*?)</a>#';
You need to escape parts of the string that you do not want parsed as special regex characters such as '?'.

preg_replace multiple times in same string

I have a text and I want to do something like Wiki code, creating links with [[]] and stuffs.
I am using this preg_replace to do that, and it seems to work:
<?=preg_replace("/\{\{([^\*]+)\|([^\*]+)\|([^\*]+)\}\}/", "<a href='$1.php#$2'>$3</a>", $conditions['pattern']); ?>
The problem is that when I have this text "can[not] build at %{{types|location|location}}% %{{some|other|stuff}}%" it outputs this:
can[not] build at %stuff%
It's like only the last one gets replaced, but wrong.
Any idea? Thanks
Fixed!
I changed the regular expression to /\{\{([a-zA-Z]+)\|([a-zA-Z]+)\|([a-zA-Z ]+)\}\}/ and now it works :D

preg_replace "%2520" with a space

I have a blog page on my website where a user edit's a post by going to a URL like this... http://www.example.com/blog?edit=blog post here. The script used to replace the spaces with %20 like it should but now it is replacing the spaces with %2520 and now the script can't search the database because there is no post called blog20post20here. I was going to go down the path of preg_replace, so I tried this...
preg_replace("/%2520/"," ",$_GET['edit']);
but that didn't seem to work.
I have never used preg_replace() and I just now read up on it in the manual. If someone could either point me down the right path and or show me how to correctly use preg_replace that would be awesome.
Sounds like you're double-escaping somewhere when generating the urls. %25 is the coding for the % character, so it sounds like it's going from %20 to %2520.
As an aside, there's better ways to decode that url (urldecode() for example), so perhaps preg_replace isn't really necessary...
EDIT: oh, and you should just use urlencode to generate the url in the first place.
For %2520
<?php echo urldecode(urldecode($_GET['edit'])); ?>
For %20
<?php echo urldecode($_GET['edit']); ?>

Regex for parsing URL

I am working on parsing urls in the form of http://site.com/page/var1/var2 and I am using the following regex to do so:
([^/]+)
Now I understand that this takes all values that are not / but I want to also be able to take variables with escaped slashes in them. When I paste these into my browser they end up looking like this: http://site.com/page/var1//stillvar1/var2 which is equal to var1/stillvar1 and var2. My question is bascially how can I modify this regex equation to catch all values which are not / unless / is followed by a slash.
Hopefully I'm being clear.
Thanks in advance!
How about the following regex?
((//|[^/])+)
Dont clearly understand your requirement but as far as i understand try this (tested in robular)
^\w+\:\/{2}(\w+\.)+\w+(\/{1}\w+)+$

Regex to change spaces in images into entities

I'm having a lot of difficulty matching an image url with spaces.
I need to make this
http://site.com/site.com/files/images/img 2 (5).jpg
into a div like this:
.replace(/(http:\/\/([^\s]+\.(jpg|png|gif)))/ig, "<div style=\"background: url($1)\"></div>")
Here's the thread about that:
regex matching image url with spaces
Now I've decided to first make the spaces into entities so that the above regex will work.
But I'm really having a lot of difficulty doing so.
Something like this:
.replace(/http:\/\/(.*)\/([^\<\>?:;]*?) ([^\<\>?:;]*)(\.(jpe?g|png|gif))/ig, "http://$1/$2%20$3$4")
Replaces one space, but all the rest are still spaces.
I need to write a regex that says, make all spaces between http:// and an image extension (png|jpg|gif) into %20.
At this point, frankly not sure if it's even possible. Any help is appreciated, thanks.
Trying Paolo's escape:
.escape(/http:\/\/(.*)\/([^\<\>?:;]*?) ([^\<\>?:;]*)(\.(jpe?g|png|gif))/)
Another way I can do this is to escape serverside in PHP, and in PHP I can directly mess with the file name without having to match it in regex.
But as far as I know something like htmlentities do not apply to spaces. Any hints in this direction would be great as well.
Try the escape function:
>>> escape("test you");
test%20you
If you want to control the replacement character but don't want to use a regular expression, a simple...
$destName = str_replace(' ', '-', $sourceName);
..would probably be the more efficient solution.
Lets say you have the string variable urlWithSpaces which is set to a URL which contains spaces.
Simply go:
urlWithoutSpaces = escape(urlWithSpaces);
What about urlencode() - that may do what you want.
On the JS side you should be using encodeURI(), and escape() only as a fallback. The reason to use encodeURI() is that it uses UTF-8 for encoding, while escape() uses ISO Latin. Same problems applies for decoding.
encodeURI = encodeURI || escape;
alert(encodeURI('image name.png'));

Categories