Passing escaped special characters for use as a URL through macros

From an earlier question on Escaping special characters for use as a URL, using \string to escape the special URL characters in the second example works great:

\DefineMyFormatLinkParameters{%
{people.brunel.ac.uk}{http://people.brunel.ac.uk/\string~mastmmg/ssguide/set\string_work.html\string#4\string_32},
{Yahoo}{http://www.yahoo.com}
}


\DefineMyFormatLinkParameters defines a list of parameters in\MyFormatLinkParameters:

\newcommand*{\DefineMyFormatLinkParameters}[1]{%
}%


\newcommand*{\DefineMyFormatLinkParametersNew}[2][TypeB]{%
%\edef\MyFormatLinkParametersNew{#2}% Don't assign it here directly,
% but pass it along
\IfStrEqCase{#1}{%
}[\typeout{*** ERROR: Unknown link type: #1}]%
}%


which passes the list of parameters to another macro, \DefineMyFormatLinkParametersTypeA or \DefineMyFormatLinkParametersTypeB, there is a problem in processing the special characters. Things works fine for cases which do not use special TeX characters as per the MWE below:

Notes

• In the MWE, the problem case is commented out in the call to \DefineMyFormatLinkParametersNew. Uncomment the people.brunel.ac.uk line to reproduce the problem.

Code:

\documentclass{article}
\usepackage{url}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{xstring}

s%   #1 =* not used yet
O{}% #2 = optional title
m%   #3 = Mandatory title
}{%
\par
\hspace*{1.0cm}\href{#4}{#3\IfValueT{#2}{~(#2)}}%
}%

}%

%% one extra level of macro processing based on the optional #1 parameter
\newcommand*{\Title}{}%
}%
}%
%\edef\MyFormatLinkParametersNew{#2}% Don't assign it here directly,
% but pass it along
\IfStrEqCase{#1}{%
}[\typeout{*** ERROR: Unknown link type: #1}]%
}%

\begin{document}
% ------------------------------- First version: works fine.
{people.brunel.ac.uk}{http://people.brunel.ac.uk/\string~mastmmg/ssguide/set\string_work.html\string#4\string_32},
{Yahoo}{http://www.yahoo.com}
}

\typeout{DEBUG: "\x"}
}%

%------------------------------- Second version: Not working if uncomment
%                                people.burnel.ac.uk.  Only difference
%                                is that it has one extra level of macro
%                                processing which causes some problem!
%{people.brunel.ac.uk}{http://people.brunel.ac.uk/\string~mastmmg/ssguide/set\string_work.html\string#4\string_32},
{Yahoo}{http://www.yahoo.com}
}

\bigskip\Title:%
\typeout{DEBUG: "\x"}
}

\end{document}


Not sure I'd use xstring if you already have l3 and pgf loaded but anyway:

\documentclass{article}
\usepackage{url}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{xstring}

s%   #1 =* not used yet
O{}% #2 = optional title
m%   #3 = Mandatory title
}{%
\par
\hspace*{1.0cm}\href{#4}{#3\IfValueT{#2}{~(#2)}}%
}%

}%

%% one extra level of macro processing based on the optional #1 parameter
\newcommand*{\Title}{}%
}%
}%
%\edef\MyFormatLinkParametersNew{#2}% Don't assign it here directly,
% but pass it along
\edef\tempz{\unexpanded{#2}}%
\IfStrEqCase{#1}{%
}[\typeout{*** ERROR: Unknown link type: #1}]%
}%

\begin{document}
% ------------------------------- First version: works fine.
{people.brunel.ac.uk}{http://people.brunel.ac.uk/\string~mastmmg/ssguide/set\string_work.html\string#4\string_32},
{Yahoo}{http://www.yahoo.com}
}

\typeout{DEBUG: "\x"}
}%

%------------------------------- Second version: Not working if uncomment
%                                people.burnel.ac.uk.  Only difference
%                                is that it has one extra level of macro
%                                processing which causes some problem!