3

In Wordpress, in a custom template, I have a lot of lines like that:

<h3>
<a name="_Toc531441816"></a> == 0
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.2.1 -&nbsp;&nbsp;&nbsp;&nbsp;Blabla..."
</h3>

There is one character and 4 characters &nbsp;
This code come from an export from Word.

I don't need to change the beginning of the line, but I need to replace

&nbsp;&nbsp;`3.2.1 -&nbsp;&nbsp;&nbsp;&nbsp;Blabla...

with

&nbsp;&nbsp;`3.2.1 - Blabla...

I think that I must use regex, but I didn't find the right expression.
I can't find how to search the expression -&nbsp;&nbsp;&nbsp;&nbsp; in order to replace it by -

Thank you to help me,
Regards, Bruno

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Bruno Bros
  • 87
  • 1
  • 9

2 Answers2

4

If the string to replace is exactly –&nbsp;&nbsp;&nbsp;&nbsp; you do not need regex, you can just search and replace it normally.

However, the regex would have been –(?:&nbsp;){4}

Liinux
  • 173
  • 7
  • In fact, the code that I use in function.php is : function replace_text_wps($str){ $str = preg_replace('/-/','+', $str); return $str; } add_filter('the_content', 'replace_text_wps'); Nothing changes. I think that I must replace '-' by the unicode. I tried chr(150) with no result. I don't know what to do... – Bruno Bros Dec 03 '18 at 12:46
  • Is it perhaps an [em dash](https://stackoverflow.com/questions/10357622/whats-the-ascii-character-code-for)? – Liinux Dec 03 '18 at 12:56
  • Perhaps, I don't know. In fact, I tried chr(8212) => don't work. Do you know the right expression with "preg_replace" ? – Bruno Bros Dec 03 '18 at 13:09
  • I found the code to replace "–" : – I found the code to replace " " : \xc2\xa0 I use this code to replace "–": function replace_text_wps($str){ $str = mb_ereg_replace("–", "+", $str ); return $str; } add_filter('the_content', 'replace_text_wps'); Can you help me to fix the expression for "– " (– followed by 4 non breakable spaces). In fact, how to concatenate : "–" "\xc2\xa0" "\xc2\xa0" "\xc2\xa0" "\xc2\xa0" Thank you for your help, – Bruno Bros Dec 03 '18 at 14:00
  • 1
    The right expression is : $str = mb_ereg_replace('–(?:\xc2\xa0){4}', '+', $str ); – Bruno Bros Dec 03 '18 at 14:48
  • So, it was in fact an [en dash](http://www.thepunctuationguide.com/en-dash.html), good to know. Answer updated. – Liinux Dec 03 '18 at 14:51
1

The right expressions to replace strings "– " and "● " by "– " and "● " are: For "non breakable space": $str = mb_ereg_replace('–(?:\xc2\xa0){4}', '– ', $str ); For "black circle": $str = mb_ereg_replace('\xE2\x97\x8F(?:\xc2\xa0){4}', '● ', $str );

Bruno Bros
  • 87
  • 1
  • 9