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'] ); ?>
Related
In WordPress, entitled My Town - Charleston, SC
In PHP 7.2, I would like to take this title and turn it into charleston,-sc
To do this, I'm using str_replace, strtolower, and trim.
$title = trim(str_replace('my town -', '', strtolower(get_the_title())));
$locationTag = str_replace(' ', '-', $title);
This works in regular PHP if I replace get_the_title with the string itself, but when I use the code on a WordPress template page, it does not replace my town - with a blank. Instead it only replaces spaces with dashes and makes everything lowercase.
Why is this happening? Does WordPress do some weird order of operations here to optimize stuff?
It's because for some reason get_the_title function converts '-' into '–'. I am not sure if you can see it clearly but the second dash is a bit wider.
I recreated the problem in my local environment and found this weird behavior.
var_dump( get_the_title() ); echo '<br>';
var_dump( $post->post_title ); echo '<br>';
var_dump( "My Town - Charleston, SC" ); echo '<br>';
You can see it clearly here in this screenshot-
I want to replace all special character (in array), I used htmlspecialchars, but it does not work I found empty result !!
this is my instruction:
str_replace( array('è','é','ê','ë'),
array('e','e','e','e'),
htmlspecialchars(strtolower("Elément")) );
thank's for helping...
Short answer: you must use mb_strtolower instead strtolower,
run the snippet below you will find why:
<?php
$a = str_replace( array('è','é','ê','ë'), array('e','e','e','e'), htmlspecialchars(strtolower("Elément")) );
echo "\n0.".$a;
echo "\n1.".htmlspecialchars(strtolower("Elément"));
echo "\n2.".strtolower("Elément");
echo "\n3.".mb_strtolower("Elément");
echo "\n4.".htmlspecialchars(mb_strtolower("Elément"));
$a = str_replace( array('è','é','ê','ë'), array('e','e','e','e'), htmlspecialchars(mb_strtolower("Elément")) );
echo "\n5.".$a;
see also enter link description here
You could use a slugger, such as https://packagist.org/packages/javiereguiluz/easyslugger
i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>
I have a form where users can quote others using bbcode. When somebody push quote button, the textarea value is :
[quote user=User date=1348246887 post=301]
User post text
[/quote]
And now, my code to transform into a block is :
$post = preg_replace("/\[quote user=(.*) date=(.*) post=(.*)](.*)\[\/quote\]/Uis", "<div class=\"quote\"><p>Quote by \\1 at time : \\2 </p><span>\\4</span></div>", $post);
How can i convert the time to date into preg_replace ? In preg_replace i can't do it, because the value of \2 is not set.
Try something like this (I added "test" inside the link, so you could see the link--not sure what you wanted there, but a non-breaking space won't make the link visible.) I used htmlentities for security in case the "subiect" $_GET variable (which maybe you meant to be "subject"?) contained markup or quotes. And of course, you can customize the date() string first argument to your needs. Finally, I added \s+ to allow for more flexible whitespacing. I also changed delimiter '/' to '#' so you don't need to escape '/' within the regex.
Updated for older PHP compatibility:
<?php
$post = <<<HERE
[quote user=User date=1348246887 post=301]
User post text
[/quote]
HERE;
// ] (just adding this comment to fix SO syntax colorer)
function replacer ($matches) {
return '<div class="quote"><p>Quote by '.$matches[1].' at time : '.
date('Y m d', $matches[2]).'<a href="index.php?subject='.
htmlentities($_GET['subiect'], ENT_COMPAT, 'UTF-8').
'&post='.$matches[3].'">test </a></p><span>'.
$matches[4].'</span></div>';
}
$post = preg_replace_callback(
'#\[quote\s+user=(.*)\s+date=(.*)\s+post=(.*)](.*)\[/quote\]#Uis',
'replacer',
$post
);
var_dump($post);
?>
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