0

I get this string "Holder – 2pcs" from my Wordpress post title using the get_the_title() function then I use str_replace to replace the "–" character but no luck!

str_replace("–","-","Holder – 2pcs");

any help appreciated!

Edit:

(Response to comments)

I had to save the text from $title1=get_the_title(); to .txt file and I noticed that the – saved as – in the txt file ... then I replaced str_replace("–","-","Holder – 2pcs") and it works! the problem is that in my wordpress databse the title contains - char as it should but then when I use get_the_title(); function of wordpress in my code to retrieve the title I get the - char as – which is eventually – I dont know why get_the_title(); causing this issue!

Any thoughts?

Community
  • 1
  • 1
stefanosn
  • 3,264
  • 10
  • 53
  • 79
  • try this `str_replace(" – ","-","Holder – 2pcs")` – Rakesh Jakhar Jun 23 '19 at 11:36
  • 3
    *It doesn't work* could never be a descriptive sentence. What is the exact problem? https://3v4l.org/UFBuh – revo Jun 23 '19 at 11:37
  • 1
    `—` `–` – u_mulder Jun 23 '19 at 11:37
  • Try copying the character from your string to be sure you are using the right one. – Irfanullah Jan Jun 23 '19 at 11:42
  • 2
    Looks like a character confusion issue: different characters that look very similar or even identical, depending on the font you are using to display those characters. Typically happens in UTF environments. Easiest to find out what character you actually deal with is by saving the title to a file and opening that with a `hexeditor`. That tells you exactly what character is coded there, its numerical equivalent. – arkascha Jun 23 '19 at 11:48

2 Answers2

1

Your issue is caused by your "-" character being something else that looks the same.

Step 1:

Ensure that everything is using the same Character set, from your MySQL to your PHP to your input text.

$title1 = iconv(mb_detect_encoding(get_the_title(), mb_detect_order(), true), "UTF-8", get_the_title());

(reference)

Step 2:

Ensure that what you convert is the raw string and not an HTML encoded output

$title2 = html_entity_decode($title1, ENT_NOQUOTES | ENT_HTML5, "UTF-8");

Step 3:

Run the str_replace() function as originally attempted. If there are a range of possible "dash" characters then you can build an array:

$dashes = ['–','–','—','-'];
$title3 = str_replace($dashes,"-",$title2);

(reference)

Martin
  • 22,212
  • 11
  • 70
  • 132
0

The code you've shared does work:

var_dump(str_replace("–","-","Holder – 2pcs"));

string(13) "Holder - 2pcs"

If it doesn't, they you're actually running something different. Most likely, your input data contains white space or HTML entities and you're looking at it through browser glasses.

Trying further inspecting your input data with e.g.:

header('Content-Type', 'text/plain');
var_dump("Holder – 2pcs", bin2hex("Holder – 2pcs"));
string(15) "Holder – 2pcs"
string(30) "486f6c64657220e280932032706373"
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Well i had to save the text i get from get_the_title(); to .txt file and i noticed that the – saved as – in the txt file ...then i replaced str_replace("–","-","Holder – 2pcs") and it works! the problem is that in my wordpress databse the title contains - char as it should but then when i use get_the_title(); function of wordpress in my code to retrieve the title i get the - char as – which is eventually – i dont know why get_the_title(); causing this issue! any thoughts? – stefanosn Jun 23 '19 at 13:21
  • Well, that [function](https://developer.wordpress.org/reference/functions/get_the_title/) of yours appears to be doing [a lot of complex vaguely documented stuff](https://developer.wordpress.org/reference/functions/apply_filters/) but I want to think it makes sense to those familiar with the platform (I'm not). – Álvaro González Jun 23 '19 at 13:49
  • function is not mine its from wordpress anyway. thank you! – stefanosn Jun 23 '19 at 13:55