0

I asked a question similar to this recently, but I believe this is a different question.

IE, Safari and Chrome break hyphened words at the end of a line. Firefox will break hyphened words at the end of a line but will no leave 4 or less characters on the previously line. Instead it will drop the hyphened word to the next line.

I would like to use function.php to prevent a phone# (123) 456-7890 from breaking at the hyphen (between two numbers) when it reaches the end of a line in IE, safari and chrome . I have tried this and similar ways without success:

function non_breaking_telhyp($content){
return str_replace('[0-9]-[0-9]', '[0-9]- [0-9]', $content);
}

add_filter('the_content', 'non_breaking_telhyp');

Thank you for your time Jerry

  • 1
    possible duplicate of [No line-break after a hyphen](http://stackoverflow.com/questions/7691569/no-line-break-after-a-hyphen) – Zantier Jan 06 '15 at 10:53
  • Hi, I don't believe it's a duplicate because in my situation I only have the option of using php- any type of inline code does not help me. Jerry – user3166179 Jan 06 '15 at 11:18
  • It seems to me a combination of asking: How to I prevent a line break after a hyphen in HTML? And how do I output this HTML in php? – Zantier Jan 06 '15 at 11:28
  • Using function.php in a wordpress child page, I am able to prevent a phone# (123) 456-7890 from breaking at the space after the area code but have not been successful using function.php preventing it from breaking at the hyphen. Using html, a tag or a class at the location of the phone# itself is not an option for me. Thank you for your time Jerry – user3166179 Jan 06 '15 at 15:07
  • Did you try the linked answer: replacing the hyphen with a non-breaking hyphen `‑`? – Zantier Jan 06 '15 at 15:32
  • If you mean (123) 456‑7890, that is not an option for me. I need a phone# to be typed into wordpress' visual editor and not break at the hyphen. Jerry – user3166179 Jan 06 '15 at 16:25
  • Have you tried using `str_replace` like you did in your code sample? – Zantier Jan 06 '15 at 16:38
  • Sorry, I don't understand the question, jerry – user3166179 Jan 06 '15 at 18:28
  • You gave a code sample in your question that uses `str_replace`. I haven't used wordpress, so I'm not familiar with the `add_filter` function you used, but could you perhaps use it to replace hyphens with `‑`? – Zantier Jan 07 '15 at 10:27
  • Both below work with the above code to prevent a phone # from breaking at the end of a line: return str_replace(') ', ') ', $content); and return str_replace('3-', '3‑', $content); What I can't do is get the second str_replace to work for (0-9). Thank you for your time. Jerry – user3166179 Jan 25 '15 at 13:09
  • You seem to be using regular expression, which the docs tell me would need `preg_replace` instead of `str_replace`. http://php.net/manual/en/function.preg-replace.php – Zantier Jan 26 '15 at 09:55
  • This works with the above code, return preg_replace('/[0-9]-[0-9]/', 'a', $content); but I have spent numerous hours trying to use this to prevent a hyphen from breaking between two numbers without success. Thank you for your time. Jerry – user3166179 Jan 28 '15 at 12:27
  • It seems you just need the correct regex strings. The `preg_replace` doc has all the information you need, I think. This might do it: `return preg_replace('/([0-9])-([0-9])/', '$1‑$2', $content);`. The brackets around `[0-9]` allow you to use references in the replacement pattern. As per the doc, if your php version is less than PHP 4.0.4, you will have to replace `$` with `\\\\ ` in the replacement pattern. – Zantier Jan 28 '15 at 13:24
  • Using (123) 456-7890, return preg_replace('/[0-9]-[0-9]/', '&1‑&2', $content); returns (123) 45&1-&2890 and replacing & with \\\\ in the replacement pattern returns (123) 45\1-\2890 and both break at the hyphen. Thank you for your time. Jerry – user3166179 Jan 29 '15 at 11:09
  • Using (123) 456-7890, return preg_replace('/[0-9]-[0-9]/', '$1‑$2', $content); returns (123) 45-890 and replacing $ with \\\\ in the replacement pattern returns (123) 45\1-\2890 and both do not break at the hyphen. Sorry about the confusion. Jerry – – user3166179 Jan 29 '15 at 20:31
  • The code from your last post, return preg_replace('/([0-9])-([0-9])/', '$1‑$2', $content); works perfect. Thank you for your time. I apologize for the confusion and any extra time I may have cost you. Thank you for your help. Jerry – user3166179 Feb 02 '15 at 15:44

0 Answers0