Hide Links In Forums - php

I've tried to hide link from none registered members, the BBcode works and it hides, but the HTML code doesn't work.
For example,
[link=http://www.brandbucket.com/]Brand Bucket[/link]
This hides well.
On the other hand..
http://www.char5.com
This doesn't hide at all, the hyperlink works fine.
Here's the code below for any help please, thanks.
$text = preg_replace("/\[file\=(.*?)\](.*?)\[\/file\]/is", $rep, $text);
$text = preg_replace("/\[link\=(.*?)\](.*?)\[\/link\]/is", $rep, $text);
$text = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/is", $rep, $text);
$text = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<,]*)#is", "\\1".$rep, $text);
$text = preg_replace("#(^|[\n \]])((www|ftp)\.[\w+-]+?\.[\w+\-.]*(?(?=/)(/.+?(?=\s|,\s))|(?=\W)))#is", "\\1".$rep, $text);

My suggestion would be to use an if else statement and echo the elements on to the page if conditions are met.
For example on my website I have a logout button. It only shows if a user is logged in.
<?php
if(/*user is logged in*/) {
echo 'This is where the element code is placed';
}
else {
//user is not logged in so don't echo the element
}
?>
This php block will be placed inside the container element where the element you want to show would normally be. Note that if the else section of the statement is empty you can leave it out.
This can also work for a specific id or class you want to apply to an element (like changing the background colour of a div) by inserting the block where the id or class definition is.
<div id="testdiv" <?php if(/*condition*/) {echo 'class="addedclass"';} ?> ><div>

Related

Php check for specific word and change color

I have never programmed in php before but I just found out that i need to edit the wp-admin page of a word press site and it MUST be done in php.
Actually what i need to do is change the color of a specific word.For example I need "cars" to be always on red. In jQuery that could be something like this:
$('p:contains("cars")').css('color', 'red');
Can anyone help me to write this in php please
You can use strpos and do something like that :
$text = "some text containing cars word";
if (strpos($text , 'cars') !== false) {
$style = 'style="color:red"';
}
else {
$style = "";
}
echo "<p ".$style.">".$text."</p>";
If the text contains "cars" word, strpos() will return the position of the word in the text. Else it will return false.
if you want to replace just words cars colored by red use preg_replace with flag g in pathern Regex to match all the words (cars) in the paragraph and replace them with red colored span. if you want change all paragraph use the code suggested by Titi
<?php
$paragraph = "<p>we all know cars faster than bikes!<p>";
echo preg_replace('/(cars)/g', '<span style="color:red;">$0</span>', $paragraph);
//<p>we all know <span style="color:red;">cars</span> faster than bikes!<p>
?>
It seems that you can use js script in the WordPress admin_enqueue_scripts.
Place your script into your plugin folder and then use it like that :
function my_enqueue($hook) {
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts', 'my_enqueue');
This way, it calls a PHP function that calls a js file
Here is the official documentation about that : https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
There are some posts about that at the bottom of the page.

basic php 'if then' URL link in wordpress

I'm trying to write code that will display a URL link if there one present from a form sumbmission;
If > [a link exists]
then [display the text 'more info' with the href link wrapped around it]
I've confused myself mixing wordpress and php, and can't quite get it. Any help would be great.
This question isn't very specific, but the pseudo-code I can offer is this:
<?php if (isset($_GET['url'])): ?>
Read more
<?php endif; ?>
Are you looking to do something like this to the comments displayed on a post?
Comment: "I like https://www.google.com/" becomes "I like more info".
If that's the case, perhaps adding a filter to functions.php to search for and replace URL might do the trick:
// define the get_comment_text callback
function filter_get_comment_text( $comment_comment_content, $comment, $args ) {
// Regular expression to find URL
$pattern = '/(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)/i';
// Replace url with linked "more info"
$replacement = 'more info';
// Find matches & replace
$newcomment = preg_replace($pattern, $replacement, $comment_comment_content);
// Return the comment
return $newcomment;
};
// add the filter
add_filter( 'get_comment_text', 'filter_get_comment_text', 10, 3 );

correct regex synthax in custom bbcode apllication

I wish to integrated the prism synthax highlighter into my custom built php/mysql CMS; however i am having some issues with regex synthax.
This is what i wish to accomplish:
Allow users to post
text alone,
or code and text but never code alone.
Code may be preceded by text or text may be included after the code.
My PHP Code is below:
function bbcode2html($var)
{
// [code]
$var = preg_replace('/\[code](.+?)\[\/code]/si',
'<section class="language-markup">
<pre><code>$1</code></pre>
</section>', $var);
return $var;
}
$var = '[code]<!DOCTYPE html>[/code] THis is text';
// verify content input
if(!preg_match("/(w+?)|\[code](.+?)\[\/code]/si", $var))
{
echo 'The code tags can not be empty!';
}
elseif(!preg_match("/(w+?)|\[code](.+?)\[\/code](w+)/si", $var))
{
echo 'Your post contains only code, please add some text';
}else{
echo $var = bbcode2html(htmlentities($var));}
With the present code above, this is what i have observed the following:
When text alone is posted, i get this feedback 'The code tags can not be empty!'
When text and code are posted, i get this feed back 'Your post contains only code, please add some text'
I therefore need clues as to the right regex synthax that will enable me achieve these two objectives:
Allow users to post text alone, or code and text but never code alone.
Code may be preceded by text or text may be included after the code.
Thanks.
What about doing two checks:
Verify the string contains one [[:alnum:]], after stripping all [code]...[/code] and trimming.
Check for empty code tags
See example on eval.in
$var = '[code]<!DOCTYPE html>[/code] THis is text';
// code alone or nothing
if(!preg_match('~[[:alnum:]]~', trim(preg_replace('~\[code\].*?\[/code\]~', "", $var))))
{
echo 'Please add some text...';
} else {
// empty code tags
if(preg_match('~\[code\]\s*\[/code\]~', $var))
{
echo 'The code tags can not be empty!';
// fine
} else {
echo "wohoOoo it works!";
}
}

How to chain in phpquery (almost everything can be a chain)

Good day everyone,
I'm very new with phpquery and this is my first post here at stackoverflow for a reason that i cant find the correct for syntax for the phpquery chaining. I know someone knows what i been looking for.
I only want to remove the a certain div inside a div.
<div id = "content">
<p>The text that i want to display</p>
<div class="node-links">Stuff i want to remove</div>
</content>
This few lines of codes works perfect
pq('div.node-links')->remove();
$text = pq('div#content');
print $text; //output: The text that i want to display
But when I tried
$text = pq('div#content')->removeClass('div.node-links'); //or
$text = pq('div#content')->remove('div.node-links');
//output: The text that i want to display (+) Stuff i want to remove
Can someone tell me why the second block of code is not working?
Thanks!
The first line of code will only work if your trying to remove the class from div.node-links, it won't remove the node.
If you are trying to remove the class you need to change it from:
$text = pq('div#content')->removeClass('div.node-links');
// to
$text = pq('div#content')->find('.node-links')->removeClass('node-links')->end();
which will output:
<div id="content">
<p>The text that i want to display</p>
<div>Stuff i want to remove</div>
</div>
As for the second line of code.. I'm not exactly sure why it is not working, it seems like your not selecting .node-links but I was able to get the desired results using these.
// $markup = file_get_contents('test.html');
// $doc = phpQuery::newDocumentHTML($markup);
$text = $doc->find('div#content')->children()->remove('.node-links')->end();
// or
$text = pq('div#content')->find('.node-links')->remove()->end();
// or
$text = pq('div#content > *')->remove('.node-links')->parent();
Hope that helps
Since remove() does not take any parameter, you can do:
$text = pq('div#content div.node-links')->remove();

Get content in faster way from url using php

I am using php, I want to get the content from url in faster way.
Here is a code which I use.
Code:(1)
<?php
$content = file_get_contents('http://www.filehippo.com');
echo $content;
?>
Here is many other method to read files like fopen(), readfile() etc. But I think file_get_contents() is faster than these method.
In my above code when you execute it you see that it give every thing from this website even images and ads. I want to get only plan html text no css-style, images and ads. How can I get this.
See this to understand.
CODE:(2)
<?php
$content = file_get_contents('http://www.filehippo.com');
// do something to remove css-style, images and ads.
// return the plain html text in $mod_content.
echo $mod_content;
?>
If I do that like above then I am going in wrong way, because I already get the full content in variable $content and then modify it.
Can here is any function method or anything else which get the directly plain html text from url.
Below code is written only to understanding, this is not the original php code.
IDEAL CODE:(3);
<?php
$plain_content = get_plain_html('http://www.filehippo.com');
echo $plain_content; // no css-style, images and ads.
?>
If I can get this function it will be much faster than others. Can it is possible.
Thanks.
Try this.
$content = file_get_contents('http://www.filehippo.com');
$this->html = $content;
$this->process();
function process(){
// header
$this->_replace('/.*<head>/ism', "<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE html PUBLIC '-//WAPFORUM//DTD XHTML Mobile 1.0//EN' 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head>");
// title
$this->_replace('/<head>.*?(<title>.*<\/title>).*?<\/head>/ism', '<head>$1</head>');
// strip out divs with little content
$this->_stripContentlessDivs();
// divs/p
$this->_replace('/<div[^>]*>/ism', '') ;
$this->_replace('/<\/div>/ism','<br/><br/>');
$this->_replace('/<p[^>]*>/ism','');
$this->_replace('/<\/p>/ism', '<br/>') ;
// h tags
$this->_replace('/<h[1-5][^>]*>(.*?)<\/h[1-5]>/ism', '<br/><b>$1</b><br/><br/>') ;
// remove align/height/width/style/rel/id/class tags
$this->_replace('/\salign=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sheight=(\'?\"?).*?\\1/ism','');
$this->_replace('/\swidth=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sstyle=(\'?\"?).*?\\1/ism','');
$this->_replace('/\srel=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sid=(\'?\"?).*?\\1/ism','');
$this->_replace('/\sclass=(\'?\"?).*?\\1/ism','');
// remove coments
$this->_replace('/<\!--.*?-->/ism','');
// remove script/style
$this->_replace('/<script[^>]*>.*?\/script>/ism','');
$this->_replace('/<style[^>]*>.*?\/style>/ism','');
// multiple \n
$this->_replace('/\n{2,}/ism','');
// remove multiple <br/>
$this->_replace('/(<br\s?\/?>){2}/ism','<br/>');
$this->_replace('/(<br\s?\/?>\s*){3,}/ism','<br/><br/>');
//tables
$this->_replace('/<table[^>]*>/ism', '');
$this->_replace('/<\/table>/ism', '<br/>');
$this->_replace('/<(tr|td|th)[^>]*>/ism', '');
$this->_replace('/<\/(tr|td|th)[^>]*>/ism', '<br/>');
// wrap and close
}
private function _replace($pattern, $replacement, $limit=-1){
$this->html = preg_replace($pattern, $replacement, $this->html, $limit);
}
for more - https://code.google.com/p/phpmobilizer/
you can use regular expression to delete css-script's tags and image's tags, just replace those codes with blank space
preg_replace($pattern, $replacement, $string);
for more detail of function go here: http://php.net/manual/en/function.preg-replace.php

Categories