J. Doe
J. Doe

Reputation: 69

PHP Remove All Characters After Final - Character

I'm trying to list some titles on my site, but these titles also contain information I want to hide, such as:

Need For Speed - Update 1.02

One Piece: Pirate Warriors 3 - 0

Toukiden: Kiwami - Demo - 0

As you can see, these titles contain numbers - {num} at the end, or Update {num}.

I've tried to remove these using this:

$displayname = substr($item['displayname'], 0, strpos($item['displayname'], ' - '));

This works for some titles, but titles without containing these characters will be removed like LIMBO.

I'm trying to find a perfect solution for this. I've thought about using regex to find either - Update {random} and - {random} at the end of the title to replace it with nothing, but I'm unsure how to execute this.

I hope someone can help me further!

Upvotes: 1

Views: 42

Answers (3)

Sergio Susa
Sergio Susa

Reputation: 264

if you have to delete the last part of the string after the last '-', you can use this:

$newTitle = substr($title, 0, strripos($title, '-') -1 );

check the docu:

substr and strripos

Hope that help you.

Upvotes: 1

StackSlave
StackSlave

Reputation: 10627

I think you need to do something like this:

function stripEnd($title){
  return preg_replace('/\-\s*(Update\s*)?[0-9.]+$/', '', $title);
}
$need = stripEnd('Need For Speed - Update 1.02');
$piece = stripEnd('One Piece: Pirate Warriors 3 - 0');
$kiwami = stripEnd('Toukiden: Kiwami - Demo - 0');

Upvotes: 0

Forbs
Forbs

Reputation: 1276

you can try this

 $temp = explode("-",$item['displayname']);
 $displayname = $temp[0];

This will strip any info after any '-' and any without a '-' will still work.

Upvotes: 0

Related Questions