2011-05-19 49 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];