php
2014-06-24 251 views 1 likes 
1

我想從以下字符串刪除特殊字符@從一個字符串中刪除特殊字符

$string="Test <a href='/group/profile/1'>@Test User</a> another test <a href='/group/profile/2'>@User Test</a>"; 

預期輸出:

<a href='/group/profile/1'>Test User</a> another test <a href='/group/profile/2'>User Test</a>" 

在這裏,我需要檢查每一個錨標籤字符串中和只需要找到帶有href中的word profile的錨標籤,並且需要從鏈接文本中刪除@。如果字符串中的錨標籤外部有任何@,則不應刪除它,只有anc中的@ hor標記需要刪除。

+0

輸出缺少前導' 「測試」'以及;預期? –

回答

3

使用正則表達式:

$string = preg_replace('/(<a href.*profile.*>)@/U', '$1', $string); 

心靈的ungreedy(U)改性劑。

+0

真棒..這工作 – user1851420

0

嘗試:

$string = <<<EOT 

@Do not remove 
Test <a href='/group/profile/1'>@Test User</a> another test <a href='/group/profile/2'>@User Test</a> 
@metoo 

EOT; 


$string = preg_replace('/(\<a\s+.+?\>.*?)(\@)(.*?\<\/a\>)/','$1$3',$string); 

var_dump($string); 

這可以是大串車雖然

http://us3.php.net//manual/en/function.preg-replace.php http://us3.php.net/manual/en/reference.pcre.pattern.syntax.php

0

試試這個:

//從字符串中刪除@

$string=str_replace('@','',$string); 
的PHP

//編碼串使用BASE64_ENCODE功能

$string=base64_encode($string); 

//使用PHP的BASE64_DECODE函數解碼串

echo $string=base64_decode($string);   //Expected output 
相關問題