Viewed   75 times

I'm just wondering how I could remove everything after a certain substring in PHP


Posted On April 6th By Some Dude

I'd like to have it so that it removes all the text including, and after, the sub string "By"



$variable = substr($variable, 0, strpos($variable, "By"));

In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.

Friday, August 12, 2022

You need a Ajax call to pass the JS value into php variable

JS Code will be (your js file)

var jsString="hello";
    url: "ajax.php",
    type: "post",
    data: jsString

And in ajax.php (your php file) code will be

$phpString = $_POST['data'];     // assign hello to phpString 
Thursday, November 10, 2022

alert('my name is: <?php echo $man; ?>' );

Friday, August 19, 2022
new_str = str.slice(0..(str.index('blah')))

Wednesday, November 30, 2022

You are looking for str.rsplit(), with a limit:

print x.rsplit('-', 1)[0]

.rsplit() searches for the splitting string from the end of input string, and the second argument limits how many times it'll split to just once.

Another option is to use str.rpartition(), which will only ever split just once:

print x.rpartition('-')[0]

For splitting just once, str.rpartition() is the faster method as well; if you need to split more than once you can only use str.rsplit().


>>> x = ''
>>> print x.rsplit('-', 1)[0]
>>> 'something-with-a-lot-of-dashes'.rsplit('-', 1)[0]

and the same with str.rpartition()

>>> print x.rpartition('-')[0]
>>> 'something-with-a-lot-of-dashes'.rpartition('-')[0]
Thursday, December 22, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :