Replace white spacing - php

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>

Related

Just a space for words and remove if you only have one space [duplicate]

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

Trim whitespace from esc_attr() in WordPress?

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

preg_replace of php code

I am writing an application that will look at a single record, obtain values from about 12 flags (0 or 1), look up those flags against a status table (in MySQL) and return a variable called $status_message which is in that table.
In this table I need to have hyperlinks (working fine) but also echo some variables, i.e.
You have no bids for {{$row->_item_name}}
or
View this item now by clicking here
Now I need item name and the other example to be translated into <?php echo $row->_item_name; ?>
I have tried a preg_replace with the following:
<?php
$find = array('/{{/', '/}}/');
$replace = array('<?php echo ', ' ?>');
echo preg_replace($find, $replace, $status_message);
?>
but this is not working.
Can anyone advise how I can get the desired result and 'echo' the variable in the MySQL field?
Had a brainwave. Much simpler,
instead of $row->_item_name I just put {{itemname}} in the string. I then use the following code:
<?php
$message_buyer = str_replace('{{itemname}}', $row->_item_name , $message_buyer);
echo $message_buyer;
?>
so no need to have <?php calls in the string at all.

Replace specific strings in Wordpress title

I'm trying to replace a string within a Wordpress title. I need replace two specific keywords in the title. I don't wish to change the title for the whole page only for this specific instance. I can't seem to get this code to work and I'm not sure how to add addition strings to replace. Thank you for the help - I'm a PHP novice.
<?php
$wptitle = the_title();
$wptitle = str_replace('Download', '', $wptitle);?>
<?php echo $wptitle; ?>
This works:
<?php
$wptitle = get_the_title();
$wptitle = str_replace(array('REPLACESTRING1', 'REPLACESTRING2'), '', $wptitle);?>
<?php echo $wptitle; ?>

How to add images in latest news joomla

I need to get separated the image and text of the article, the problem is that they are in one field of the database, so how can i make that in the view i can put the image separated from the text? because i can't put the image and the text togheter, is a mess
<? foreach ($list as $item) : ?>
<?
$year = date('Y', strtotime($item->created));
$month = date('m', strtotime($item->created));
$day = date('d', strtotime($item->created));
$shortDescription = cutString($item->introtext, 100);
?>
<div class="footerItem">
<?= $day; ?>-<?= $month; ?>-<?= $year; ?>
<p><?= $item->title; ?></p>
<p>
<?= $shortDescription; ?>
</p>
</div>
<?php endforeach; ?>
so i need to put the image like this $item->image - this is what i want, but in joomla latestnews_mod, there is no field with image, and i don't know if i need to write my own, or edit this module, or i need to use other module for this ? And also i cannot get the field $item->category that i need, there is only the CAT_ID, and how can i get the category field??
I've lost touch with Joomla a bit, so it's entirely possible this is not the easiest way, but could you use regex to pull out the image from the intro-text if it's not specifically given?
You'd need a pattern something like:
<img\b[^>]+?src\s*=\s*['"]?([^\s'"?#>]+)
That's taken from this question: Regex to find the first image in an image tag in an HTML document
EDIT: Example using preg_match:
<?php
// This is the Regex pattern from above.
$pattern = '/<img\b[^>]+?src\s*=\s*[\'"]?([^\s\'"?#>]+)/';
// Perform the search, matches will be stored in $matches
preg_match($pattern, $item->introtext, $matches );
echo "Image src is: " . $matches[1] . "\n";
?>
Do note that this is relatively untested, may not work and if it does, may miss cases with bad markup!

Categories