So I am trying to figure out how I can:-
Remove all white-space; and
Remove all other characters e.g. ()
I have a phone number which is pulled from user profile and I wish to make this a clickable link.
OLD:
<div class="phone heading-font"><?php echo esc_attr($user_fields['phone']); ?></div>
NEW:
<?php echo esc_attr($user_fields['phone']); ?>
Problem is if user enters their number as (03) 1234 1234 it won't work unless I remove whitespace and the () area code fields.
I wasn't sure how I could use trim with esc_attr?
Use str_replace() like below:-
str_replace(array( '(', ')',' ' ), '', esc_attr($user_fields['phone']);
Like:-
<?php echo esc_attr($user_fields['phone']); ?>
Example:- https://eval.in/753919
Related
There seems to be a bug in a Wordpress PHP function that leaves whitespace in front of the title of the page generated by <?php echo wp_title(''); ?> I've been through the Wordpress docs and forums on that function without any luck.
I'm using it this way <body id="<?php echo wp_title(''); ?>"> in order to generate an HTML body tag with the id of the page title.
So what I need to do is strip that white space, so that the body tag looks like this <body id="mypage"> instead of this <body id=" mypage">
The extra white space kills the CSS I'm trying to use to highlight menu items of the active page. When I manually add a correct body tag without the white space, my CSS works.
So how would I strip the white space? Thanks, Mark
Part Two of the Epic
John, A hex dump was a good idea; it shows the white space as two "20" spaces. But all solutions that strip leading spaces and white space didn't.
And, <?php ob_start(); $title = wp_title(''); ob_end_clean(); echo $title; ?>
gives me < body id ="">
and <?php ob_start(); $title = wp_title(''); echo $title; ?>
gives me < body id =" mypage">
Puzzle. The root of the problem is that wp_title has optional page title leading characters - that look like chevrons - that are supposed to be dropped when the option is false, and they are, but white space gets dumped in.
Is there a nuclear option?
Yup, tried them both before; they still return two leading spaces... arrgg
Strip all whitespace from the left end of the title:
<?php echo ltrim(wp_title('')); ?>
Strip all whitespace from either end:
<?php echo trim(wp_title('')); ?>
Strip all spaces from the left end of the title:
<?php echo ltrim(wp_title(''), ' '); ?>
Remove the first space, even if it's not the first character:
<?php echo str_replace(' ', '', wp_title(''), 1); ?>
Strip only a single space (not newline, not tab) at the beginning:
<?php echo preg_replace('/^ /', '', wp_title('')); ?>
Strip the first character, whatever it is:
<?php echo substr(wp_title(''), 1); ?>
Update
From the Wordpress documentation on wp_title, it appears that wp_title displays the title itself unless you pass false for the second parameter, in which case it returns it. So try:
<?php echo trim(wp_title('', false)); ?>
ltrim()
ltrim($str)
Just to throw in some variety here: trim
<body id="<?=trim(wp_title('', false));?>">
Thanks for this info! I was in the same boat in that I needed to generate page ids for CSS purposes based on the page title and the above solution worked beautifully.
I ended up having an additional hurdle in that some pages have titles with embedded spaces, so I ended up coding this:
<?php echo str_replace(' ','-',trim(wp_title('',false))); ?>
add this to your functions.php
add_filter('wp_title', create_function('$a, $b','return str_replace(" $b ","",$a);'), 10, 2);
should work like a charm
I am getting About Us through
<?php echo ($contents->title); ?>.
But I want to convert this to About_us. How can I achieve this? After this clicking on that anchor it showing About us in the URL but I want to remove that space between them or change it like About_us.
<a class="smoothscroll" href="#<?php echo ($contents->title); ?>" scroll-to="#para_1">
You can use str_replace to replace all spaces in your string with underscore like below:
<?php $contents->title = str_replace(' ','_',$contents->title); ?>
If $contents->title contains About Us then above code will replace About Us with About_Us
Hi I need a simple way with inline PHP to search/replace the URL string, then output the result into the HTML
Our checkout cart redirects people to our post-purchase page, and something like the following ends up in the URL bar:
http://example.com/postpurchasepage.php?amount=$9.99&firstname=Billy&lastname=Bob
More often, based on the browser, they end up with this in their URL bar:
http://example.com?amount=%249.99&firstname=Billy&lastname=Bob
(converts the $ into %24 )
Yes, the cart sends the $ symbol along inside the parameter value. No I don't want it, nor the %24. So, the challenge is to remove both the $ symbol or the %24 (whichever happens),
Right now I am trying to pull the amount value into the page using <?php echo #$_GET['amount']; ?>
For example:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
However that outputs:
<p>Hey Billy !</p>
<p>Thanks for donating %249.99</p>
I want it to output:
<p>Hey Billy !</p>
<p>Thanks for donating 9.99</p>
(or whatever the amount value is that was passed from the cart, without the $ symbol or the %24)
Like I said, basically I am looking for a way to search the URL bar for whatever I define (multiple 'or' type searching), replace with whatever I define, and output the result into the html
Any help appreciated!
You might be looking for urldecode()
echo urldecode("%24"); // $
Then you can strip off the leading $ (if any):
$string = (strpos($string)===0) ? substr($string, 1) : $string;
Manual: http://php.net/manual/en/function.urlencode.php
There are two ways (or more - depending on your creativity & scenario) to get this done;
If you have access to the value of amount before the redirecting occurs, you can strip off the "$" sign and just send in the numeric value and use it there.
Otherwise, you can get the value on the page, and then as willoller mentioned, urldecode it and you can then strip the any leading character that is not numeric. (Not sure if performance would be an issue but that's what worked for me several times before)
Here's a sample for your code:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$Get_Amount = urldecode($_GET['amount']);
// WHILE LOOP = TO VALIDATE IF VALUE IS NUMERIC
while (is_numeric($Get_Amount)==false) {
if (is_numeric(substr($Get_Amount, 0,1))){
// DO NOTHING = First Character Numeric
// Might be an issue if you have characters in between the number
// THIS WILL CAUSE RE-LOOP PROBLEM (Dependent on your scenario)
}else{
// TRIM OFF THE FIRST CHARACTER
$Get_Amount = substr($Get_Amount, 1);
}
}
?>
<p>Thanks for donating <?php echo $Get_Amount; ?></p>
Hope this helps! Good Luck!
OR -----------------------
You can use the preg_replace function like this:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$String_Get_Amount = urldecode($_GET['amount']);
// Here you can add the characters that you wanna delete
// You should not delete anything that is not numeric cause then it would even replace the decimal point
$String_Search_For_Pattern = '/[$]/';
// Replace it with nothing
$String_To_Replace = '';
$String_Get_Amount = preg_replace($String_Search_For_Pattern, $String_To_Replace, $String_Get_Amount);
?>
<p>Thanks for donating <?php echo $String_Get_Amount; ?></p>
I have the following sting that I need to localize for translation
<?php echo 'Click here to go and wacth the video.' ?>
My problem is the here part in the middle of the sentence. I did try to break it up like this
<?php printf(__('Click %s to go and wacth the video.', 'pietergoosen'), 'here'); ?>
This works, but the word here inside the <a> tag then can't be translated.
Any suggestions how this can be solved
You can split the tag into two separate strings:
printf(__('Click %shere%s to go and wacth the video.', 'pietergoosen'), '', '');
I'm needing to replace white spacing with a plus sign "+" for the code displayed below.
I'm in the process of modifying some code which generates the label and url for products displayed in my catalog. The problem I face is that my current code doesn't do the replacement. Can someone please modify the code, replacing a spacing for a plus sign "+".
<h5><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h5>
and will return a url something like this:
http://www.efficienttrade.co.nz/catalogsearch/result/?order=relevance&dir=desc&q=potassium nitrate
However, when getName() function is used, names which have a space don't work for the generated search query. So I need to replace the space with a "+" to make the search query url work.
Thanks
As far as i understand your problem, you need to replace spaces by hypens in your product name. This can be achieved by replacing the following code in your href
...<?php echo $this->stripTags($_product->getName(), null, true); ?>...
with
...<?php echo str_replace(' ', '-', $this->stripTags($_product->getName(), null, true)); ?>...
How about the following to make you code slightly nicer (although PHP/HTML soup is never a lot of fun). The first line of PHP is the one that replaces spaces with hyphen
<?php
/*Get product name, stripped of HTML and spaces*/
$productName = str_replace(' ', '-', strip_tags($_product->getName(), null, true));
/*Assign variables rather than using same function multiple times.*/
$productAttribute = $_helper->productAttribute($_product, $_product->getName(), 'name');
/*Concatenate the URL here for easier code fixing later.*/
$url = 'http://www.efficienttrade.co.nz/catalogsearch/result/order=relevance&dir=desc&q=' . $productName;
?>
<h5>
<?php echo $productAttribute ?>
</h5>