0

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:

https://stackoverflow.com/questions/12042170/get-the-characters-before-the-last-dash

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

2 Answers 2

2

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);
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
    Commented Aug 23, 2018 at 11:42
1

I fount where were problem...

https://codex.wordpress.org/Function_Reference/wptexturize

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 and acknowledge you have read our privacy policy.

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