remove part of string after a special character

92 views Asked by At

What I am trying to do is remove a * and the number after it from a string.

$string = 'something*10';
$string2 = 'something*1';

needs to output

something
something

not

something*10
something*1
3

There are 3 answers

0
Pogrindis On BEST ANSWER
$string = current(explode('*', 'something*10'));

As found here : REF

Running Example : IDEONE

0
William Francis Gomes On

Try below code which is easy

echo $string = substr($string, 0, strpos( $string, '*'));
echo $string2 = substr($string2, 0, strpos( $string2, '*'));

Let me know if working. It should work. Thnx

Reference: substr, strpos

5
cychoi On

You can use preg_replace with this regular expression /\*\d+/ to solve your problem.

The other answers assume that the * are followed by numbers which is incorrect based on your requirement.