Removing HTML markup from href link - php

I recently made a page where I can monitor website traffic. I came so far as to replace a certain keyword with some html markup and making links clickable. But here is the problem. whenever I click a link with html markup it messes up the link with the <mark> and <b> tag. How do I remove the tags from the <a>?
Here is what I have so far:
$output = shell_exec('tail -n50 /var/log/nginx/access.website.log');
$output = preg_replace('!(((f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i', '<a target="_blank" href="$0">$0</a>', $output);
$keyword = "twitch.tv|facebook.com|4chan.org|adf.ly|j.gs|q.gs|twitter.com|goo.gl|google.com|vk.com";
$output = preg_replace("/($keyword)/i","<b><mark>$0</mark></b>",$output);
echo "<pre>Access log:<br>$output</pre>";

Related

Edit iframe content using PHP, and preg_replace()

I need to load some 3rd party widget onto my website. The only way they distribute it is by means of clumsy old <iframe>.
I don't have much choice so what I do is get an iframe html code, using a proxy page on my website like so:
$iframe = file_get_contents('http://example.com/page_with_iframe_html.php');
Then I have to remove some specific parts in iframe like this:
$iframe = preg_replace('~<div class="someclass">[\s\S]*<\/div>~ix', '', $iframe);
In this way I intend to remove the unwanted section. And in the end i simply output the iframe like so:
echo ($iframe);
The iframe gets output alright, however the unwanted section is still there. The regex itself was tested using regex101, but it doesn't work.
You should try this way, Hope this will help you out. Here i am using sample HTML remove the div with given class name, First i load the document, query and remove that node from the child.
Try this code snippet here
<?php
ini_set('display_errors', 1);
//sample HTML content
$string1='<html>'
. '<body>'
. '<div>This is div 1</div>'
. '<div class="someclass"> <span class="hot-line-text"> hotline: </span> <a id="hot-line-tel" class="hot-line-link" href="tel:0000" target="_parent"> <button class="hot-line-button"></button> <span class="hot-line-number">0000</span> </a> </div>'
. '</body>'
. '</html>';
$object= new DOMDocument();
$object->loadHTML($string1);
$xpathObj= new DOMXPath($object);
$result=$xpathObj->query('//div[#class="someclass"]');
foreach($result as $node)
{
$node->parentNode->removeChild($node);
}
echo $object->saveHTML();

Regex to find anchor tags that link to an image

I'm using this code in my WordPress theme functions.php to add a "fancybox" class to linked images. However, I do not want to add the fancybox class to images that are linked to non-image URLs.
In other words: currently the script will add the "fancybox" class to an HTML structure like this (which I do not want):
<a href="http://acme.com/a-non-image-link">
<img src="http://acme.com/image.jpg" />
</a>
Where as I only want to add the class to structures like:
<a href="http://acme.com/an-image.jpg">
<img src="http://acme.com/an-image.jpg" />
</a>
or
<a href="http://acme.com/an-image.jpg">
<img src="http://acme.com/another-image.jpg" />
</a>
How can I modify the regex expression in this code to only include anchor tags whose HREF attribute contains .jpg, .jpeg, .png, or .gif?
Thank you!
// Add fancybox class to linked images
function add_classes_to_linked_images($html) {
$classes = 'fancybox fancybox-img'; // can do multiple classes, separate with space
$patterns = array();
$replacements = array();
$patterns[0] = '/<a(?![^>]*class)([^>]*)>\s*<img([^>]*)>\s*<\/a>/'; // matches img tag wrapped in anchor tag where anchor tag where anchor has no existing classes
$replacements[0] = '<a\1 class="' . $classes . '"><img\2><span class="post-content-fb-btn"></span></a>';
$patterns[1] = '/<a([^>]*)class="([^"]*)"([^>]*)>\s*<img([^>]*)>\s*<\/a>/'; // matches img tag wrapped in anchor tag where anchor has existing classes contained in double quotes
$replacements[1] = '<a\1class="' . $classes . ' \2"\3><img\4> <span class="post-content-fb-btn"></span></a>';
$patterns[2] = '/<a([^>]*)class=\'([^\']*)\'([^>]*)>\s*<img([^>]*)>\s*<\/a>/'; // matches img tag wrapped in anchor tag where anchor has existing classes contained in single quotes
$replacements[2] = '<a\1class="' . $classes . ' \2"\3><img\4> <span class="post-content-fb-btn"></span></a>';
$html = preg_replace($patterns, $replacements, $html);
return $html;
}
add_filter('the_content', 'add_classes_to_linked_images', 100, 1);
The following should work with one caveat. The link needs to be on a separate line to the image tag, or the file extension of the image will create a match.
/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i
Maybe someone can improve on it rather than posting sarcastic comments :)

Wordpress, editing blog index link

Im trying to edit my webisites blog that I inherited from a creator I have no contact with. I have been fiddling with the site for a little more than a week but I don't know how to edit the links in the blog index.
Here is the link to my site's blog:
http://bestdetails.com/blog/
As you can see the box is a link to the whole article, and in the article if you scroll down you can see the tags for the post.
I want the tags inside the box to show on the blog index page instead.
I want to put the tags where the red circle is:
I know that you can edit the blog in the blog.php, but I don't know what code or where in the HTML text to put that code.
In order to access the post tags, you have the function get_tags();
It is called this way:
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
The place to edit will depend on your Theme, it can be in the index, the page.php or the blog.php. Take a look at your Settings -> Reading and see what page is acting as home and which is acting as blog. After that go to pages and see what template is associated, if any.

How to Replace Code Between A Tags Only When Link is Nofollow

Trying to replace nofollow text and image links with some HTML code (turning nofollow links into CSS/javascript links), been using preg_match and preg_replace for a while, but realised it fails under certain scenarios earlier today.
My PHP basic code (it's more complicated than this, what I'd been using pasted at the bottom) but this is the core code, the add_filter part is WordPress specific:
add_filter('the_content', 'st_nofollow_content', 99);
function st_nofollow_content($content) {
$content = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $content);
return $content;
}
This is the simplified version, the code I've been using tried to take into account the issue described below, but it doesn't work under all circumstances so it's not ideal.
This works as expected as long as there's only one link or all links include rel="nofollow".
If there's a link with no rel="nofollow" attribute followed by a link with rel="nofollow" the two links merge together.
anchor one blah, blah... anchor two
Output
<span class="affst" title="tests" id="url-one">anchor one</a> blah, blah... <a href="url-two">anchor-two</span>
The output should be:
anchor one blah, blah... <span class="affst" title="tests" id="url-two">anchor two</span>
I understand the problem, but not sure there's a preg_replace solution?
This code is what I'd been using and works most of the time other than when there's a mixture of links with and without nofollow:
if (preg_match('/<a href="(.*?)<\/a>(.*?)<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', $content)) {
$content = preg_replace('/(.*?)<\/a>/i', '<a href="$1$2<span class="affst" title="tests" id="$3">$4</span>', $content);
} else {
$content = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $content);
}
Spent half the day on this and if I understand correctly it's not possible with the above type of code.
What should my approach be for only wanting to replace the code when
1st opening (a) tag : url : rel="nofollow" : closing (/a) tag associated with the 1st opening a tag?
Thanks
David
Edit: Looks like I should be using DOM which I'm not as familiar with. got this far:
function st_nofollow_content($content){
$dom = new DOMDocument();
$dom->loadHTML( $content );
$dom->preserveWhiteSpace = false;
$alinks = $dom->getElementsByTagName('a');
foreach ($alinks as $alink) {
$rel = $alink->getAttribute('rel');
if( $rel = 'nofollow') {
$alink = preg_replace('/<a href="(.*?)" rel="nofollow">(.*?)<\/a>/i', '<span class="affst" title="tests" id="$1">$2</span>', $alink);
}
}
$content = $dom->saveHTML();
return $content;
}
Doesn't work, throws out a fair number of warnings
DOMDocument::loadHTML(): Unexpected end tag : div in Entity : related to $dom->loadHTML... line
Object of class DOMElement could not be converted to string : related to $alink = preg_replace.... line
and has no impact on the links.
David

create anchors in a page with the content of <h2></h2> in PHP

Well I have a html text string in a variable:
$html = "<h1>title</h1><h2>subtitle 1</h2> <h2>subtitle 2</h2>";
so I want to create anchors in each subtitle that has with the same name and then print the html code to browser and also get the subtitles as an array.
I think is using regex.. please help.
I think this will do the trick for you:
$pattern = "|<h2>(.*)</h2>|U";
preg_match_all($pattern,$html,$matches);
foreach($matches[1] as $match)
$html = str_replace($match, "<a name='".$match."' />".$match, $html);
$array_of_elements = $matches[1];
Just make sure that $html has the existing html before this code starts. Then it will have an <a name='foo' /> added after this completes, and $array_of_elements will have the array of matching text values.

Categories