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
Wit开发者_JAVA技巧h 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?
$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
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
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];
精彩评论