如何替换帖子标题中的3个字符串?
我有这个代码:
但现在它可以工作了。当我只刺一针时,效果非常好。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打开 php 标签声明以下代码应由服务器解释为 PHP(而不仅仅是 HTML 并传递到客户端)
下面的行声明一个带有标识符(名称)
$wptitle
的变量将其值设置为等于使用参数get_the_ID
调用函数get_the_title
的结果。这些函数必须在其他地方声明。下一个将变量
$wptitle
重新分配为相同的字符串,其中字符串“foo”被字符串“bar”替换如果您想替换更多字符串,那么您可以重复这一行
,这样这种情况下,所有出现的字符串“foo”和“bar”都将被替换。
或者,您可以将数组传递给
str_replace
以一次性执行多个替换。下一行打印出变量
$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)
The following line declares a variable with the identifier (name)
$wptitle
and sets its value equal to the result of calling the functionget_the_title
with the argumentget_the_ID
. these functions must be declared elsewhere.The next one reassigns the variable
$wptitle
to be the same string with the string "foo" being replaced by the string "bar"If you want to replace some more strings then you can repeat this line
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.The next line prints out the contents of the variable
$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它几乎可以工作,但它不会替换连字符“-”
我的意思是Word1和Word2被替换,但第三个字符“-”没有被替换,我不知道为什么......
It almost works, but it does not replace hyphen "-"
What I mean is Word1 and Word2 is replaced but third sting which is "-" is not replaced and I dont know why...