So, I'm running into this problem with this relatively complicated wildcard 301 redirect. My .htaccess-foo is terrible, so I'm hoping for some help here!
I need to change any uppercase to lowercase.
I need to change "gfoldername" to "g"
I need to replace any "-" with "_"
To show an example, this is what I'm trying to accomplish
example.com/gfoldername/Another-Folder/
to
example.com/g/another_folder/
I've tried quite a bit with no luck. Again, any help is greatly appreciated!
EDIT:
Ended up resolving this myself with php. For those interested:
$urls = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$url = explode('/',$urls);
$f = $url[4];
$f = strtolower($f);
$f = str_replace("-", "_", $f);
$url = "http://example.com/g/".$f;
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$url);
Related
When concatenating mysql regex character classes in php they disappear from the resulting string i.e.:
$regexp_arr = array('(word1)', '(word2)');
$value = 'word3';
$regexp_str = implode('[[:space:]]', $regexp_arr);
$v1 = '[[:<:]](' . $value . ')';
echo $regexp_str;
// gives
'(word1)(word2)';
// instead of
'(word1)[[:space:]](word2)'
echo $v1;
// gives
'(word3)'
//instead of
'[[:<:]](word3)'
I've tried with double quotation marks ", the result still the same.
Is there a special way to concatenate this in php? Why are the '[[:char_class:]]' getting stripped?
server php version is 5.6.36
In MODX, [[ and ]] are special characters used to indicate they are tags MODX needs to process. Even when you echo or retrieve it from the database, MODX will process them when rendering.
For debugging, you can follow-up your echo with an exit().
echo $regexp_str;
exit();
That short-circuits MODX and gives you the actual value of the string including the square brackets.
If you want the value to be visible in a MODX-rendered resource or template, then you'll have to replace them with their html entities first:
$regexp_str = str_replace(['[',']'], ['[', ']'], $regexp_str);
I am having a perl script in which i am giving path to directory as input.
Directory has xml files inside it.
In my code i am iterating through all the xml files and creating absolute path for all xml files. Code is working fine.
#!/usr/bin/perl
use File::Spec;
$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}
my $dirPath = $ARGV[0];
opendir(DIR, $dirPath);
my #docs = grep(/\.xml$/,readdir(DIR));
foreach my $file (#docs)
{
my $abs_path = join("",$dir,$file);
print "absolute path is $abs_path";
}
Question i have here is,
joining $dirPath and $file with no separator which means that $dirPath must end in a "/". So is there any way or built in function in perl which take cares of this condition and replaces the join method.
All i want is not to worry about the separator "/". Even if script is called with path as "/test/dir_to_process" or "/test/dir_to_process/", i should be able to produce the correct absolute path to all xml files present without worrying about the separator.
Let me know if anyone has any suggestions.
Please take heed of the advice you are given. It is ridiculous to keep asking questions when comments and answers to previous posts are being ignored.
You must always use strict and use warnings at the top of every Perl program you write, and declare every variable using my. It isn't hard to do, and you will be reprimanded if you post code that doesn't have these measures in place.
You use the File::Spec module in your program but never make use of it. It is often easier to use File::Spec::Functions instead, which exports the methods provided by File::Spec so that there is no need to use the object-oriented call style.
catfile will correctly join a file (or directory) name to a path, doing the right thing if path separators are incorrect. This rewrite of your program works fine.
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Functions 'catfile';
if (#ARGV != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}
my ($dir_path) = #ARGV;
my $xml_pattern = catfile($dir_path, '*.xml');
while ( my $xml_file = glob($xml_pattern) ) {
print "Absolute path is $xml_file\n";
}
The answer is in the documentation for File::Spec, e.g., catfile:
$path = File::Spec->catfile( #directories, $filename );
or catpath:
$full_path = File::Spec->catpath( $volume, $directory, $file );
This will add the trailing slash if not there:
$dirPath =~ s!/*$!/!;
this is my .htaccess file
..
RewriteRule ^songbook(?:/([a-z]))?/?$ music/songbook.php?char=$1 [L]
with that rule i can use the following url's:
http://www.example.com/songbook
http://www.example.com/songbook/
http://www.example.com/songbook/a
http://www.example.com/songbook/a/
So far so good. The problem occurs when i try the get the char in PHP, like so:
if (isset($_GET['char'])) {
echo 'FOUND a char';
} else {
echo 'there is NO char';
}
For some reason, it always finds a char, even when there is no char provided.
My rewrite rule says that the char is optional, but i guess i'm doing something wrong.
How do i write the rewriterule so that the char is optional so the if condition in PHP works?
Thanks
Problem is that your rewrite rule always defines char in query string even if it's an empty string.
You have two options :
1- Edit your php:
if (isset($_GET['char']) && !empty($_GET['char'])) {
echo 'FOUND a char';
} else {
echo 'there is NO char';
}
2- Edit your .htaccess:
You can change the .htaccess:
RewriteRule ^songbook/?$ music/songbook.php [L]
RewriteRule ^songbook/([a-z])/?$ music/songbook.php?char=$1 [L]
I am moving aspx files from an old system to the new one using Powershell. I need to parse the page and change href of hyperlink tags as following.
old system
href=/ranet/templates/page____9372.aspx
will be in new system
/newfolder/folder1/9372.aspx
Try this
$input = "href=/ranet/templates/page____9372.aspx"
$new = "/newfolder/folder1/"
$array = $input.Split('_')
$array2 = ($array[$array.Count-1]).Split('.')
$newLine = $new+$array2[0]+".aspx"
$input
$newLine
Actually this is not such a great answer as you have to ensure that there are no other underline characters in the page!
I'm using Windows 7. I have a bunch of text files, each containing one email message. Each starts this way:
FROM: Person
TO: Another Person
DATE: 01-Jan-11 at 18:12:00
SUBJECT: Whatever
I want to rename these files so that their names look like this:
2011-01-01 18.12 Email from Person to Another Person re Whatever.txt
Batch programming is all I know, and I don't know it very well. For purposes of restraining this to a project that I can understand quickly, I think my best solution will be to extract the essential data into a text file that I can then massage into a batch renaming file.
In that case, what I'm looking for is a batch file that will extract the data into single lines in a text file that I can then massage into shape with global edits. In other words, I think I'm looking for text lines in this format:
[current filename] [extracted date and time string] [from] [to] [subject]
Example:
file01.txt 01-Jan-11 at 18:12:00 from Person to Another Person re Whatever
If I've got lines like that, I can parse them into renaming commands pretty quickly in Excel.
Thanks!
Given that your using Windows 7, I thought I'd suggest an alternative. Windows Powershell is a a very useful command tool that can be used for a ton of stuff. I think I solved your complete problem:
$folder = "C:\..."
$regex = "FROM: (.*) TO: (.*) DATE: (.*) at (.*) SUBJECT: (.*)"
$files = Get-ChildItem $folder *.txt
ForEach ($file in $files) {
$line = (Get-Content $file.FullName -TotalCount 1)
$match = ([regex]$regex).matches($line)[0]
$date = [DateTime]($match.Groups[3]).Value + [TimeSpan]($match.Groups[4]).Value
$from = ($match.Groups[1])
$to = ($match.Groups[2])
$subject = ($match.Groups[5])
# You can change the naming format in the brackets below
Rename-Item $file.FullName -NewName ( $date.ToString("yyyy-MM-dd_HH-mm-ss") + " Email From " + $from + " to " + $to + " RE " + $subject)
}
It makes a few assumptions (like a match will always be found). You can easily adjust naming format and other things. Save this code as a script (.ps1) and run it in the Powershell prompt (powershell.exe)