- Posts: 68
COMMUNITY FORUM
Forbid links in commens?
- web_dev
-
Topic Author
- Offline
- Senior Member
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
- Posts: 8743
Please Log in or Create an account to join the conversation.
- web_dev
-
Topic Author
- Offline
- Senior Member
- Posts: 68
i want to strip links from the comments text
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
php.net/manual/en/function.strip-tags.php
eg:
<?php echo strip_tags($comment->commentText); ?>
The code for the comments list is located in the item.php file.
Please Log in or Create an account to join the conversation.
- BrethrenArchives.Com
-
- Offline
- Junior Member
- Posts: 37
Thank you commenting on this topic. Here is a link to a comment on my site. Am I also seeing that the HTML on this page will disappear?
Where do I find the item.php file please?
Cordially,
A. Wayne Webb
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
I tried using the following code, but it did not convert the special characters to HTML entities:
<?php echo strip_tags($this->item->extraFields->NAME->value); ?>
Note: Current PHP version: 5.3.29
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
php.net/strip_tags
You can also use srt_replace php.net/str_replace
So you can add rel="nofollow" if you do not want to remove the links.
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
My goal is to echo Extra Fields for meta purposes, which would require the removal of special characters especially quotes and apostrophes.
I thought I could use the existing strip_tags function, but it is not working in my item template override.
Can you please provide the specific code needed to accomplish tag stripping of an Extra Field. Thanks :)
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Yes you can strip tags in an extrafield.
Strip_tags will remove (as delete) all your HTML tags (not strings).
If you want to convert it to entities, you need to use another PHP function - htmlentities php.net/htmlentities
Finally this function won't probably help you since what you need is a string replacer.
This is what you should use -> php.net/str_replace
Check the "Example #1 Basic str_replace() examples" section which is exactly what you need.
It will remove every character you do not need.
This method can be used in pretty much all text-related elements of K2.
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
I tried the following, but is does not work:
<?php
$cleanfield = $this->item->extraFields->NAME->value;
$cleanfield = htmlspecialchars($cleanfield, ENT_QUOTES);
echo $cleanfield;
?>
Please provide cut & paste code :)
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
PS. if this is intended for metadata where each character counts, 160 is the limit, I would personally strip them. htmlentities() will increase the character count.
This should do the trick.
gist.github.com/kricore/7abc76e04c1bc9235ce2
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
I also provided the specific code to use an Extra Field for meta data
My very first Gist post :)
gist.github.com/heyjoecampbell/d62f6ccc019c07284a88
As I said before, I am not a coder, just a guy inspired to create an amazing Joomla K2 Website - I GREATLY appreciate the support you have shown me since day one :)
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
Standard addCustomTag
<?php
$doc->addCustomTag('<meta name="anything" content="'.$this->item->extraFields->AAA->value.'" />');
?>
Standard addCustomTag (with str_replace)
<?
$doc->addCustomTag('<meta name="anything" content="'.str_replace($nonsafe, $safe, $this->item->extraFields->AAA->value).'" />');
?>
Here's the logic I am trying to produce:
<?php $doc->addCustomTag(
if(isset($this->item->extraFields->AAA->value)): ?><meta name="anything" content="'.$this->item->extraFields->AAA->value.'" /><?php else: ?><meta name="anything" content="'.$this->item->extraFields->BBB->value.'" />
<?php endif; ?>
); ?>
I would also like to be able to place multiple meta statements within one addCustomTag statement if possible...
<?php $doc->addCustomTag(
if(isset($this->item->extraFields->AAA->value)): ?><meta name="anything" content="'.$this->item->extraFields->AAA->value.'" /><?php else: ?><meta name="anything" content="'.$this->item->extraFields->BBB->value.'" />
<?php endif; ?>
<meta name="anything" content="something" />
<meta name="anything else" content="something else" />
if(isset($this->item->extraFields->CCC->value)): ?><meta name="anything" content="'.$this->item->extraFields->CCC->value.'" /><?php else: ?><meta name="anything" content="'.$this->item->extraFields->DDD->value.'" />
<?php endif; ?>
); ?>
I would very much appreciate if someone could provide cut & paste solutions for both with str_replace and without str_replace.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
<?php
if( isset($this->item->extraFields->AAA->value) && $this->item->extraFields->AAA->value != '' ) {
// extrafield AAA is set and has a value
echo $this->item->extraFields->AAA->value;
} else {
// no extrafield AAA or it has no value so assign the BBB instead
echo $this->item->extraFields->BBB->value;
}
?>
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
- one standard addCustomTag
- one addCustomTag with str_replace
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
<?php
if( isset($this->item->extraFields->AAA->value) && $this->item->extraFields->AAA->value != '' ) {
// extrafield AAA is set and has a value so render it as is
$doc->addCustomTag('<meta name="anything" content="'.$this->item->extraFields->AAA->value.'" />');
} else {
// no extrafield AAA or it has no value so render the str_replace code
$doc->addCustomTag('<meta name="anything" content="'.str_replace($nonsafe, $safe, $this->item->extraFields->AAA->value).'" />');
}
?>
See the comments so you can replace these blocks with the real extrafields.
Btw Nice work :)
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
Is it possible to perform the If Statement within the addCustomTag, so that it's like this...
<?php $doc->addCustomTag('<meta name="description" content="if extrafield-A has value render extrafield-A ELSE render extrafield-B" />'); ?>
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
A cleaner approach would be this ->
gist.github.com/kricore/2c9a5434748c5f5f6cf9
You can use this gist to build various examples.
Check this post also:
davidwalsh.name/php-ternary-examples
Please Log in or Create an account to join the conversation.
- Joe Campbell
-
- Offline
- Platinum Member
- Posts: 438
Currently, I am using this approach for each condition:
<?php if(isset($this->item->extraFields->AAA->value)): ?>
<?php $doc->addCustomTag('<meta name="anything" content="'.$this->item->extraFields->AAA->value.'" />'); ?>
<?php else: ?>
<?php $doc->addCustomTag('<meta name="anything" content="'.$this->item->extraFields->BBB->value.'" />'); ?>
<?php endif; ?>
I would prefer doing it all on one line, so I would not have to duplicate the addCustomTag statement (with value & else variation).
Is it possible to execute a one line statement? I would need one for each scenario, something like this...
Example: Standard addCustomTag
<?php $doc->addCustomTag('<meta name="description" content="isset(extrafield-A)
echo extrafield-A ELSE echo extrafield-B" />'); ?>
Example: Standard addCustomTag (with str_replace)
<?php $doc->addCustomTag('<meta name="description" content="isset(extrafield-A)
echo str_replace(extrafield-A) ELSE echo str_replace(extrafield-B)" />'); ?>
If so, then it would be much easier to add & modify multiple addCustomTag statements.
Plus I prefer one line statements, as they are easier for me to copy & paste for future uses.
This is what I tried so far, but does not work :(
My bad code for: Standard addCustomTag
<?php $doc->addCustomTag('<meta property="anything" content="'if(isset($this->item->extraFields->AAA->value)) {$this->item->extraFields->AAA->value} else: {$this->item->extraFields->BBB->value} endif;'" />'); ?>
My bad code for: Standard addCustomTag (with str_replace)
<?php $doc->addCustomTag('<meta property="anything" content="'if(isset($this->item->extraFields->AAA->value)) {str_replace($nonsafe, $safe, $this->item->extraFields->AAA->value)} else: {str_replace($nonsafe, $safe, $this->item->extraFields->BBB->value)} endif;'" />'); ?>
If possible - Please provide a one line code snippet for each scenario (one with str_replace & one without) that I can copy & paste :)
Please Log in or Create an account to join the conversation.