如何替换帖子标题中的3个字符串?

发布于 2025-01-16 07:14:50 字数 207 浏览 1 评论 0原文

我有这个代码:

但现在它可以工作了。当我只刺一针时,效果非常好。我在 WordPress 中工作

I have this code:

<?php $wptitle = get_the_title( get_the_ID() ); $wptitle = str_replace(" – word1 word2", "", $wptitle); echo $wptitle; ?>

But it does now work. When I put only one sting it works perfectly. I'm working in WordPress

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

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

发布评论

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

评论(2

﹏雨一样淡蓝的深情 2025-01-23 07:14:50

打开 php 标签声明以下代码应由服务器解释为 PHP(而不仅仅是 HTML 并传递到客户端)

 <?php

下面的行声明一个带有标识符(名称)$wptitle 的变量将其值设置为等于使用参数 get_the_ID 调用函数 get_the_title 的结果。这些函数必须在其他地方声明。

$wptitle = get_the_title( get_the_ID() );

下一个将变量 $wptitle 重新分配为相同的字符串,其中字符串“foo”被字符串“bar”替换

 $wptitle = str_replace("foo", "bar", $wptitle);

如果您想替换更多字符串,那么您可以重复这一行

$wptitle = str_replace("baz", "blink", $wptitle);

,这样这种情况下,所有出现的字符串“foo”和“bar”都将被替换。

或者,您可以将数组传递给 str_replace 以一次性执行多个替换。

$wptitle = str_replace(array("foo", "bar", "baz"), "", $wptitle);

下一行打印出变量 $wptitle 的内容

 echo $wptitle; 

最后这一行指示 PHP 解释器 PHP 块已经结束

?>

有关 str_replace 语义的更多信息请查看在 php 手册页

Opening php tag declares that the following code should be interpreted as PHP by the server (rather than just HTML and being passed onto the client)

 <?php

The following line declares a variable with the identifier (name) $wptitle and sets its value equal to the result of calling the function get_the_title with the argument get_the_ID. these functions must be declared elsewhere.

$wptitle = get_the_title( get_the_ID() );

The next one reassigns the variable $wptitle to be the same string with the string "foo" being replaced by the string "bar"

 $wptitle = str_replace("foo", "bar", $wptitle);

If you want to replace some more strings then you can repeat this line

$wptitle = str_replace("baz", "blink", $wptitle);

In such case all occurrences of the string "foo" and "bar" will be replaced.

Alternatively you can pass arrays to str_replace to perform multiple replacements in one go.

$wptitle = str_replace(array("foo", "bar", "baz"), "", $wptitle);

The next line prints out the contents of the variable $wptitle

 echo $wptitle; 

Finally this line instructs the PHP interpreter that the PHP block is over

?>

For more information on the semantics of str_replace have a look at the php manual page

枯叶蝶 2025-01-23 07:14:50

它几乎可以工作,但它不会替换连字符“-”

<?php $wptitle = get_the_title( get_the_ID() ); $wptitle = str_replace(array("Word1", "Word2", "-"), "", $wptitle); echo $wptitle; ?>

我的意思是Word1和Word2被替换,但第三个字符“-”没有被替换,我不知道为什么......

It almost works, but it does not replace hyphen "-"

<?php $wptitle = get_the_title( get_the_ID() ); $wptitle = str_replace(array("Word1", "Word2", "-"), "", $wptitle); echo $wptitle; ?>

What I mean is Word1 and Word2 is replaced but third sting which is "-" is not replaced and I dont know why...

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