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
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
2
Answers
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...
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