2017-12-02 111 views
0

在WordPress的,但在PHP每個正則表達式匹配更普遍,下面這個suggestion,我想回調在字符串

  1. 轉換電子郵件地址mailto鏈接和
  2. 編碼的mailto與str_13鏈接()
  3. 要通過javascript解碼客戶端。

我一日數做精

add_filter('the_content', 'make_clickable',  12); 

,我還不知道是否三號計數,這將工作

document.write("<n uers=\"znvygb:[email protected]\" ery=\"absbyybj\">Fraq n zrffntr</n>".replace(/[a-zA-Z]/g, 
function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);})); 

我想我要什麼要做的只是在替換之後的每一次出現中,而不是preg_replace注入str_13()。我發現this question並試圖答案傳遞給我的情況:

add_filter('the_content', 'email_encoder', 99); 

function email_encoder($content) { 
    return preg_replace_callback('`\<a([^>])href\=\"mailto\:([^">])\"([^>]*)\>`ism', 'encode_str_13', $content); 
} 

function encode_str_13($matches) 
{ 
    return str_13($matches[1]);  
} 

這改變不了什麼,我很茫然,爲什麼。我懷疑這是我從here或數組項目地址獲得的正則表達式,如果我稍微改變它,字面上什麼都不會顯示,直到我恢復更改(我似乎對我的想法並不正確)。

你能幫我編碼所有的mailto鏈接並糾正我的javascript來解碼他們嗎?

感謝

編輯:這是一個例子$內容:

<p>text<br> 
text<br> 
text<br> 
numbers<br> 
<a href="mailto:....>...</a><br> 
<a href="...." rel="nofollow">....</a></p> 

而事實上我確實意味着str_rot13()而不是str_13()

EDIT2:

的JS我經過更多的擺弄之後就是這樣的:

var collection = jQuery(".encoded"); 
collection.each(function() { 

var encoded = jQuery(this).html(); 

var decoded = encoded.replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}); 
jQuery(this).html(decoded); 
jQuery(this).prop('class', 'decoded') 

}); 
+0

你能提供樣品'$ content'?我也不熟悉'str_13',那是什麼?也許它應該是http://php.net/manual/en/function.str-rot13.php? – chris85

+0

啊!你是對的,如果我得到了正確的正則表達式,這會讓我感到困擾,謝謝! – user148585

回答

0

你的正則表達式是有點不對,這裏是一個正確的(DEMO):

`\<a([^>])href\=\"mailto\:([^">]+)\"([^>]*)\>`ism 

而且在回調使用2指標,如:

function encode_str_13($matches) 
{ 
    return str_13($matches[2]);  
} 
+0

謝謝,這真是快速而簡潔。我真的錯過了第二組中的+號嗎?!並感謝您糾正該陣列。 – user148585

+0

結束語:您的正則表達式和數組地址正確地編碼了電子郵件地址。我更進一步:'return preg_replace_callback('/ \ ] +)href \ = \「mailto \:([^>] +)\」([*>] *)\>(。*?)\ <\/a\>/is','encode_str_13',$ content);'和'return「」「。str_rot13($ matches [0])。「」;'編碼整個''將被解碼和reclassed解碼爲JS(見我的文章) – user148585