PHP: 在特殊位置的字符串变量中添加连字符

PHP: add hyphen in string-variable at special position

我有一个变量,它是一个类似于 "necessary" 的字符串。现在我想在一个特殊的位置添加一个连字符:比如 "neces-sary"。如何修改字符串,使其在输出变量时总是中断?

我用 CSS 试过(用 "hyphens: auto"),但这不是我想要的。

你可以使用str_replace():-

<?php

echo $string = str_replace("neces","neces-","necessary");

输出:- https://eval.in/841897

我还通过 php 函数 substr 找到了我的问题的另一个答案。 感谢@Rhopercy,他有使用这个功能的想法。

echo substr("necessary",0,5).'-'.substr("necessary",5);

输出:https://eval.in/841935