Extra backslash needed in PHP regexp pattern
문제
When testing an answer for another user's question I found something I don't understand. The problem was to replace all literal \t
\n
\r
characters from a string with a single space.
Now, the first pattern I tried was:
/(?:\\[trn])+/
which surprisingly didn't work. I tried the same pattern in Perl and it worked fine. After some trial and error I found that PHP wants 3 or 4 backslashes for that pattern to match, as in:
/(?:\\\\[trn])+/
or
/(?:\\\[trn])+/
these patterns - to my surprise - both work. Why are these extra backslashes necessary?
해결책
이 대신 시도해보십시오 :
var url = { UserName: ' <SPSWC:ProfilePropertyValue PropertyName="PreferredName" runat="server" id="ProfilePropertyValue6"/>'};
.
위의 작동을 먼저 확인하십시오.) 일단 이름을 가져 오면 다음을 수행하십시오.
var name = { UserName: ' <SPSWC:ProfilePropertyValue PropertyName="PreferredName" runat="server" id="ProfilePropertyValue6"/>'};
var url = "http://www.google.comsearch?q="+name;
.
또한 사용하는 속성 이름이 작동하고 ID가 올바른지 확인하는지 알지 못합니다.그래서 당신은 시도해야합니다 :
var name = { UserName: ' <SPSWC:ProfilePropertyValue PropertyName="FirstName" TitleMode="true" runat="server" />'};
var url = "http://www.google.comsearch?q="+name;
.
preffered prop은 다음과 같이 보이는가?
<SPSWC:ProfilePropertyValue PropertyName="PreferredName" ApplyFormatting="false" id="userPreferredName" runat="server"/>
.
개인적인 의견 으로이 방법을 사용하지 않을 것입니다!사용자 이름을 얻으려면 JavaScript 객체 모드를 사용해야합니다!그게 그 거기에 무엇을 위해서,)
누군가는 대부분의 일을 충분히 훌륭합니다!
var spUser;
// Ensure SP objects have been loaded before executing our code
ExecuteOrDelayUntilScriptLoaded(getSpUser,"sp.js");
function getSpUser() {
var clientContext = new SP.ClientContext.get_current();
var spWeb = clientContext.get_web();
spUser = spWeb.get_currentUser();
clientContext.load(spUser);
clientContext.executeQueryAsync(getSpUserSuccess, getSpUserException);
}
function getSpUserSuccess(sender, args) {
var curUsername = spUser.get_title();
var url = "http://www.google.comsearch?q="+curUsername;
window.location = url;
}
function getSpUserException(sender, args) {
// Do any necessary error handling.
}
.
iv 귀하의 요구에 맞게 답변을 약간 수상했습니다 :)
https://stackoverflow.com/questions/17948431/query.-String-in-JavaScript-in-SharePoint-ASPX-Page
다른 팁
Its works in perl because you pass that directly as regex pattern /(?:\\[trn])+/
but in php, you need to pass as string, so need extra escaping for backslash itself.
"/(?:\\\\[trn])+/"
The regex \ to match a single backslash would become '/\\\\/' as a PHP preg string
The regular expression is just /(?:\\[trn])+/
. But since you need to escape the backslashes in string declarations as well, each backslash must be expressed with \\
:
"/(?:\\\\[trn])+/"
'/(?:\\\\[trn])+/'
Just three backspaces do also work because PHP doesn’t know the escape sequence \[
and ignores it. So \\
will become \
but \[
will stay \[
.
Use str_replace!
$code = str_replace(array("\t","\n","\r"),'',$code);
Should do the trick