如何在 – 处拆分 WordPress 标题?在 PHP 中?

发布于 2025-01-03 00:20:59 字数 239 浏览 0 评论 0原文

我正在开发我的 WordPress 博客,需要获取帖子的标题并将其拆分为“-”。事实是,它不起作用,因为在源代码中它是&ndash,当我在网站上查看结果时,它是一个“长减号”(–)。将这个长减号复制并粘贴到某个编辑器中会使其成为普通的减号 (-)。我不能在“-”或&ndash处分裂,但不知何故它一定是可能的。当我创建这篇文章时,我只是输入了“-”(减号),但在某个地方它会自动转换为 – 。

有什么想法吗?

谢谢!

I am working on my Wordpress blog and its required to get the title of a post and split it at the "-". Thing is, its not working, because in the source its &ndash and when I look at the result on the website, its a "long minus" (–). Copying and pasting this long minus into some editor makes it a normal minus (-). I cant split at "-" nor at &ndash, but somehow it must be possible. When I created the article, I just typed "-" (minus), but somewhere it gets converted to – automatically.

Any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2025-01-10 00:20:59

我想我找到了。我记得我遇到过类似的问题,当我在帖子中粘贴代码时,引号在向读者显示时会转换为 em-quad 引号。

我发现在 /wp-include/formatting.php 第 56 行(wordpress ver 3.3.1)中,它定义了一些需要替换的字符

$static_characters = array_merge( array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'\'', ' (tm)'), $cockney );
$static_replacements = array_merge( array($em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '…', $opening_quote, $closing_quote, ' ™'), $cockneyreplace );

,并在第 85 行中进行了替换

// This is not a tag, nor is the texturization disabled static strings
$curl = str_replace($static_characters, $static_replacements, $curl);

I think I found it. I remember that I have meet the similar problem that when I paste code in my post the quote mark transform to an em-quad one when display to readers.

I found that is in /wp-include/formatting.php line 56 (wordpress ver 3.3.1), it defined some characters need to replace

$static_characters = array_merge( array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'\'', ' (tm)'), $cockney );
$static_replacements = array_merge( array($em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '…', $opening_quote, $closing_quote, ' ™'), $cockneyreplace );

and in line 85 it make an replacement

// This is not a tag, nor is the texturization disabled static strings
$curl = str_replace($static_characters, $static_replacements, $curl);
近箐 2025-01-10 00:20:59

如果你想在“-”字符处分割字符串,基本上你必须用空格替换“-”。

试试这个:

$string_to_be_stripped = "my-word-test";
$chars = array('-');
$new_string = str_replace($chars, ' ', $string_to_be_stripped);
echo $new_string;

这些行在“-”处分割字符串。例如,如果您有 my-word-test,它将回显“my word test”。我希望它有帮助。

有关 str_replace 函数的更多信息,请单击此处

如果您想以 WordPress 风格执行此操作,请尝试使用过滤器。我建议将这些行放入您的functions.php 文件中:

add_filter('the_title', function($title) { 

$string_to_be_stripped = $title;
$chars = array('-');
$new_string = str_replace($chars, ' ', $string_to_be_stripped);
return $new_string;

}) 

现在,每次您在循环中使用the_title 时,标题都会被转义。

If you want to split a string at the "-" character, basically you must replace "-" with a space.

Try this:

$string_to_be_stripped = "my-word-test";
$chars = array('-');
$new_string = str_replace($chars, ' ', $string_to_be_stripped);
echo $new_string;

These lines splits the string at the "-". For example, if you have my-word-test, it will echo "my word test". I hope it helps.

For more information about the str_replace function click here.

If you want to do this in a WordPress style, try using filters. I suggest placing these lines in your functions.php file:

add_filter('the_title', function($title) { 

$string_to_be_stripped = $title;
$chars = array('-');
$new_string = str_replace($chars, ' ', $string_to_be_stripped);
return $new_string;

}) 

Now, everytime you use the_title in a loop, the title will be escaped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文