2011-05-19 61 views
1

嘿,我希望我能夠解釋這個儘可能簡單。基本上我正在編譯一個列表,爲此我需要在一個特定的觸發字後刪除一些文本。使用jQuery/PHP刪除通配符

Example of what I am trying to achieve is here

說我剛纔粘貼一串文字在我的文本框,我想要的一切,從第一個用戶名分開取出。

和例子是...

痞子reblogged這從youaref0rsaken

youaref0rsaken reblogged這從loveeoutoflust

loveeoutoflust reblogged這從yourwatchfuleye

這也是我想轉入

R-U型F-F-I-A-N

youaref0rsaken

loveeoutoflust

與使用文本框與所述輸入,按鈕和這將顯示輸出的文本框。我已經嘗試使用jQuery和PHP preg_replace來做到這一點,但無法使其正常工作。

有人能夠幫助我嗎?

+0

聽起來像你需要幫助建立正則表達式嗎?上面的例子是否準確?我的意思是你不斷地取代「從此開始」或將這種改變。而用戶名之間的界限是什麼? – matchew 2011-05-19 01:57:22

+0

它會不斷地「從* wildcard *中將此項顯示」*明顯* wildcard *被隨機用戶名替換。我希望以列表的形式做到這一點,因爲每個輸入將有大約50-100行。 – dekkytsh 2011-05-19 01:59:40

+0

@matchw when .replace(/從(。*?)/ /,「」));它只替換通配符用戶名 – dekkytsh 2011-05-19 02:05:11

回答

0

看到JavaScript的替代功能可按

string.replace(regexp/substr,newstring) 

作爲示例使用下面http://jsfiddle.net/vwhCx/ 記下代碼看看這個小提琴:如果一個正則表達式的概念是新的給你知道這裏的唯一的事情: /從/ g開始是這個// g表示全球化,或者多於一個匹配。

<div id="boxy-1"> r-u-f-f-i-a-n reblogged this from youaref0rsaken youaref0rsaken reblogged this from loveeoutoflust loveeoutoflust reblogged this from yourwatchfuleye </div> 

<button>transform</button> 
<script type="Text/javascript"> 
$(document).ready(function() { 
    $('button').click(function() { 
    //get text from box 
    var str = $('#boxy-1').text() 
    //empty box 
    $('#boxy-1').empty(); 
    //insert formatted sting 
    $('#boxy-1').append(str.replace(/reblogged this from/g, ' ')); 
    }); 
)}; 

</script> 

UPDATE

根據您的評論我已經改變了我的代碼的文本方面的工作

使用本

<textarea id="boxy-1"> r-u-f-f-i-a-n reblogged this from youaref0rsaken youaref0rsaken reblogged this from loveeoutoflust loveeoutoflust reblogged this from yourwatchfuleye</textarea> 

<button>transform</button> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $('button').click(function() { 
    //get text from box 
    var str = $('#boxy-1').text() 
    //empty box 
    $('#boxy-1').empty(); 
    //insert formatted sting 
    $('#boxy-1').val(str.replace(/reblogged this from/g, ' ')); 
}); 
}); 

新的活生生的例子:http://jsfiddle.net/K3LZU/

最後的編輯

如果你想顯示爲一個列表,或換行符嘗試海峽。取代(/自/克, '\ n' reblogged本)的\ n表示換行符

+0

@matchw這不起作用,因爲我將每次輸入新的文本到div(這就是爲什麼我需要一個textarea),甚至當我用textarea替換div時按鈕不再起作用。 – dekkytsh 2011-05-19 02:32:29

+0

好吧...我試過更新這個。 – matchew 2011-05-19 02:41:51

+0

@matchw耶,這是有效的,但我在這裏的問題是格式,我能通過電子郵件或Facebook與你聯繫,所以我可以實時聊天你?如果不是所有我可以建議是前往 http://filthycandy.co.uk/replace/test1.php 並看着我的代碼,一切工作,當我刪除輸入框中的文本,並取代它與新的文本,並按下「刪除文本」它回到我的舊文本,並刪除我剛剛添加的位。我試圖推薦你的代碼給我我需要的格式,但我做得不好。 在Facebook上添加Dekky Balboa – dekkytsh 2011-05-19 02:48:35

1
$pattern = '/reblogged this from [\w]+/'; 
$string = "r-u-f-f-i-a-n reblogged this from youaref0rsaken youaref0rsaken reblogged this from loveeoutoflust loveeoutoflust reblogged this from yourwatchfuleye"; 
echo preg_replace($pattern, '', $string); 

會給你的結果

r-u-f-f-i-a-n youaref0rsaken loveeoutoflust 
+0

http://jsfiddle.net/WGde6/9這將解釋我想要做的事情,你可以查看它,並給它一個戲劇? – dekkytsh 2011-05-19 03:25:19

0

如果它總是第一個字,你可以這樣做像這樣簡單:

$s = 'youaref0rsaken reblogged this from loveeoutoflust'; 
$a = explode(' ',$s); 
echo $a[0];