I am trying to do something but I have problem...

I wont show the list of my post, but from all titles I want remove text after last "-" (dash)

My list of posts:

  1. My experience - this boy - first day - meet
  2. My love - i do not know - second day
  3. My first time - auuu - third time

I would like to return this:

  1. My experience - this boy - first day
  2. My love - i do not know
  3. My first time

I tried this:

<?php $stringx = get_the_title();
echo preg_replace('/-[^-]*$/', '', $stringx); ?> 

But this return whole text... with the dashes....

also I tried this:


Somone could help me how to output titles of post withoust text after last '-'?


This is really a PHP question not so much a WP question -- but:

echo substr($stringx,0,strrpos($stringx,'-'));

Also I think your example #3 is wrong unless there are some cases where you want to exclude more than simply after the final dash character.

Note - if you want to exclude both that final blank space along with the final dash that immediately follows it, then change the substr length by -1 to avoid the final blank space:

echo substr($stringx,0,strrpos($stringx,'-')-1);
  • okey, something has changed, but not what I wanted My experience - this boy - first day - mee My love - i do not know - second da My first time - auuu - third tim Just cuted the last letter...
    – kibus90
    Aug 23 '18 at 11:42

I fount where were problem...


Each dash (short dash) is changing for long dash

I just wptexturize for titles by add to functions.php this code:

remove_filter('the_title', 'wptexturize');

and works well. But how can I get with long dash? I do not know... but I hope it will help someone in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.