Is there a way to remove the blank space between the words in the the_titel?
Example:
the_title() on one of my posts results in Merry Christmast
I want it to result in merrychristmast
Which means that I want to remove the blank space and use lowercase only.
Thanks
Edit: I was actually looking for a solution to the the_title-tag not the wp_title-tag. Sorry..
Doing it for wp_title();
I combined those two answers posted by Anthony and rzetterberg.
Use str_replace();. It's faster for trivial replacements than RegEx. And if you add the necessary arguments to your wp_title(); we'll end up like this. Please note, that you'll have to add the strtolower(); function so that your title is displayed in lower case only.
<?php
echo strtolower(str_replace(' ', '', wp_title('', false)));
?>
Doing it for the_title();
It's quite the same technique I posted earlier. You'll just have to change the arguments for the_title($before, $after, $echo);.
<?php
echo strtolower(str_replace(' ', '', the_title('', '', false)));
?>
Note: Instead of using the_title('', '', false) you could prepend it with a get_. It does the same and fits your needs better.
<?php
echo strtolower(str_replace(' ', '', get_the_title()));
?>
get title -> remove white spaces (preg_replace) -> to lower case (strtolower)
<?php echo strtolower(preg_replace('/\s+/', '', wp_title("",false))); ?>
No, you would have to retrieve the value and remove the spaces yourself.
Like so:
$title = wp_title("", false);
Read more about the arguments for wp_title here
Related
High,
I'm using this function
function special_chars_replace($string){
$result = preg_replace("/[&%\$##'\*:\/\"\[\]\{\};\(\)\|\\\=!\^\?`~.,\+-]/", "", $string);
return $result;
}
to delete all spaces in a css class name.
<?php echo special_chars_replace(strtolower(str_replace(" ","",$itemTags))); ?>
How do i preserve the first space before the name? So i can use it for a css class name. For example: class="tags tag01 tag02"
Just add the space before you echo the string:
<?php
echo " ".special_chars_replace(strtolower(str_replace(" ","",$itemTags)));
?>
You can use this
<?php echo implode(" ",explode(" ",$itemTags)); ?>
Regular expression is the most effective way to do this.
echo preg_replace(' +', ' ', $itemTags);
What this does is look for one or more spaces (that's what the + does), and replaces it with a single space.
Code typed from memory.
I have a text name #chatfun <chinu,25,M,123456> i want to #chatfun chinu,25,M,123456 How to replace < and > tag.
My Code :
<?php
$text = '#chatfun <chinu,25,M,123456>';
echo strip_tags($text);
?>
Above code i am getting the blank text. How to get my actual result?
Just use str_replace with the angular brackets in an array.
echo str_replace(array('>', '<'), '', $text);
Another option is to use regex with preg_replace
echo preg_replace("/[<>]+/", '', $text);
Have you tried str_replace?
$text = str_replace(array('<', '>'), '', '#chatfun <chinu,25,m,123456>');
That will replace the unwanted characters with nothing. Only caveat is that if the characters show up anywhere else, they, too, will be replaced.
<?php
$to_remove = array("<",">");
$text = '#chatfun <chinu,25,M,123456>';
echo str_replace($to_remove,"",$text);
?>
See refrence
You're getting blank text because the text between the < and > is considered part of a tag.
Use str_replace(array('<','>'),'',$text)
I'm trying to strip a variable suffix off my Wordpress titles, basically a bunch of hastags from posts imported from Instagram that are preceeded by an hyphen and I've come up with something like this:
<?php
$titleoutput = get_the_title($ID);
$titleoutputstrip = preg_replace("/\-/", "", $titleoutput);
echo $titleoutputstrip;
?>
But it doesn't seem to be working.
Can anyone help me out?
Thanks in advance!
<?php
$titleoutput = get_the_title($ID);
$titleoutputstrip = ltrim($titleoutput , '-'); // ltrim for begining rtrim for end
echo $titleoutputstrip;
?>
I finally nailed it with:
<?php
$titleoutput = get_the_title($ID);
$titleoutputstrip = preg_replace('/\ #[^.]+$/','',$titleoutput);
echo $titleoutputstrip;
?>
The hyphen was giving me problems because it's actually a dash "–" or something like that so I just set it to look for the first hashtag and erase the contents to the end.
I'm using WP-property (a wordpress plugin) and am trying to use a variable as link in a template. I'm using the following code:
<?php echo $property['download1tekst']; ?>
However, the link doesn't work as the '-' is different. How can I fix this (/replace the - with a normal -)?
Output now (ndash):
http://ouwejan.hostbeats.com/wp%E2%80%93content/uploads/2012/07/NVM_Goed%E2%80%93gevoel%E2%80%93NVM_Label_fc11.jpg
Output as it should be:
http://ouwejan.hostbeats.com/wp-content/uploads/2012/07/NVM_Goed-gevoel-NVM_Label_fc11.jpg
Thanks.
See str_replace():
http://php.net/str_replace
Example:
echo str_replace( 'badchar', '-', $variable );
Or, for more advanced replacements, preg_replace():
http://php.net/manual/en/function.preg-replace.php
First go to this page:
http://slayeroffice.com/tools/ascii/
to find the ascii number of the bad "-".
You just paste your "–" in the left box on top of the page and you click the [==] button
I think it is 8211, then you do:
<?php echo str_replace( chr(8211) , '-' , $property['download1link'] ); ?>
How can i strip html tag except the content inside the pre tag
code
$content="
<div id="wrapper">
Notes
</div>
<pre>
<div id="loginfos">asdasd</div>
</pre>
";
While using strip_tags($content,'') the html inside the pre tag too stripped of. but i don't want the html inside pre stripped off
Try :
echo strip_tags($text, '<pre>');
You may do the following:
Use preg_replace with 'e' modifier to replace contents of pre tags with some strings like ###1###, ###2###, etc. while storing this contents in some array
Run strip_tags()
Run preg_relace with 'e' modifier again to restore ###1###, etc. into original contents.
A bit kludgy but should work.
<?php
$document=html_entity_decode($content);
$search = array ("'<script[^>]*?>.*?</script>'si","'<[/!]*?[^<>]*?>'si","'([rn])[s]+'","'&(quot|#34);'i","'&(amp|#38);'i","'&(lt|#60);'i","'&(gt|#62);'i","'&(nbsp|#160);'i","'&(iexcl|#161);'i","'&(cent|#162);'i","'&(pound|#163);'i","'&(copy|#169);'i","'&#(d+);'e");
$replace = array ("","","\1","\"","&","<",">"," ",chr(161),chr(162),chr(163),chr(169),"chr(\1)");
$text = preg_replace($search, $replace, $document);
echo $text;
?>
$text = 'YOUR CODE HERE';
$org_text = $text;
// hide content within pre tags
$text = preg_replace( '/(<pre[^>]*>)(.*?)(<\/pre>)/is', '$1###pre###$3', $text );
// filter content
$text = strip_tags( $text, '<pre>' );
// insert back content of pre tags
if ( preg_match_all( '/(<pre[^>]*>)(.*?)(<\/pre>)/is', $org_text, $parts ) ) {
foreach ( $parts[2] as $code ) {
$text = preg_replace( '/###pre###/', $code, $text, 1 );
}
}
print_r( $text );
Ok!, you leave nothing but one choice: Regular Expressions... Nobody likes 'em, but they sure get the job done. First, replace the problematic text with something weird, like this:
preg_replace("#<pre>(.+?)</pre>#", "||k||", $content);
This will effectively change your
<pre> blah, blah, bllah....</pre>
for something else, and then call
strip_tags($content);
After that, you can just replace the original value in ||k||(or whatever you choose) and you'll get the desired result.
I think your content is not stored very well in the $content variable
could you check once by converting inner double quotes to single quotes
$content="
<div id='wrapper'>
Notes
</div>
<pre>
<div id='loginfos'>asdasd</div>
</pre>
";
strip_tags($content, '<pre>');
You may do the following:
Use preg_replace with 'e' modifier to replace contents of pre tags with some strings like ###1###, ###2###, etc. while storing this contents in some array
Run strip_tags()
Run preg_relace with 'e' modifier again to restore ###1###, etc. into original contents.
A bit kludgy but should work.
Could you please write full code. I understood, but something goes wrong. Please write full programming code