使用 jQuery/PHP 删除通配符

发布于 2024-11-08 15:37:18 字数 557 浏览 3 评论 0原文

嘿伙计们,我希望我能够尽可能简单地解释这一点。基本上我正在编译一个列表,为此我需要删除某个触发词后的大量文本。

我想要实现的示例在这里

假设我刚刚在文本中粘贴了一堆文本框,我希望除了第一个用户名之外的所有内容都被删除。

例子是...

流氓从 youaref0rsaken 转发了此

内容 youaref0rsaken 从 loveeoutoflust 转发了

此内容 loveeoutoflust 从 yourwatchfuleye 转发了此

内容 我想将其变成

流氓

youaref0rsaken

loveeoutoflust

使用带有输入的文本框、一个按钮和一个显示输出的文本框。我尝试使用 jquery 和 php preg_replace 执行此操作,但无法使其正常运行。

有人能帮我解决这个问题吗?

Hey guys, I hope I am able to explain this as easy as possible. Basically I am compiling a list and to do so I need to remove a load of text after a certain trigger word.

Example of what I am trying to achieve is here

Say I have just pasted a bunch of text in my text box, I want everything removed apart from the first username.

And example would be...

r-u-f-f-i-a-n reblogged this from youaref0rsaken

youaref0rsaken reblogged this from loveeoutoflust

loveeoutoflust reblogged this from yourwatchfuleye

Which I would want to turn in to

r-u-f-f-i-a-n

youaref0rsaken

loveeoutoflust

With using a textbox with the input, a button and a textbox which would display the output. I have tried doing this using jquery and php preg_replace but cannot get it to function correctly.

Is anyone able to help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心作怪 2024-11-15 15:37:18
$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 
$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);

will give you as result

r-u-f-f-i-a-n youaref0rsaken loveeoutoflust 
骄傲 2024-11-15 15:37:18

<罢工>
以 javascripts Replace funciton

string.replace(regexp/substr,newstring)

为例,使用下面的代码查看此小提琴 http://jsfiddle.net/ vwhCx/
注意:如果正则表达式的概念对您来说是新的,那么这里唯一需要知道的是:/reblogged from/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>

更新

根据您的评论

我已经更改了我的代码以在文本区域上工作使用这个

<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/

LAST EDIT

如果您尝试将其显示为列表或换行符,请尝试 str.replace(/reblogged this from/g, ' \n') \n 表示换行符


look into javascripts replace funciton

string.replace(regexp/substr,newstring)

as an example check out this fiddle using the code below http://jsfiddle.net/vwhCx/
note: if the concept of a regexp is new to you the only thing to know here: /reblogged this from/g is that the //g means globeably, or more than one match.

<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

based on your comment I have altered my code to work on a text area

use this

<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, ' '));
 });
});

new live example: http://jsfiddle.net/K3LZU/

LAST EDIT

if you are trying to display it as a list, or newlines try str.replace(/reblogged this from/g, '\n') the \n indicating a newline

凑诗 2024-11-15 15:37:18

如果它始终是第一个单词,您可以执行以下简单操作:

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

If it's always the first word, you can do something simple like this:

$s = 'youaref0rsaken reblogged this from loveeoutoflust';
$a = explode(' ',$s);
echo $a[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文