I'm trying to change every url in a paragraph to encrypted url, so i can track all clicks on outgoing links for e.g
$paragraph = "<p>This is an example <a href='example.com'>My Website</a></p>
<h2>another outgoing link</h2>";
I want to get every above url and encrypt them and replace them.
Suppose i got example.com change it into mywebsite.com/track/<?=encrypt("example.com")?>. and finally paragraph should look like this
$paragraph = "<p>This is an example <a href='mywebsite.com/track/29abbbbc-48f1-4207-827e-229c587be7dc'>My Website</a></p>
<h2></h2>";
Here is What I've tried
$message = preg_replace("/<a([^>]+)href=\"http\:\/\/([a-z\d\-]+\.[a-z\d]+\.[a-z]{2,5}(\/[^\"]*)?)/i", "<a$1href=\"encrypted url", $message);
Using preg_match or preg_replace for HTML string is a very bad idea you should use,DOMDocument
Try this code snippet here
<?php
ini_set('display_errors', 1);
$paragraph = "<p>This is an example <a href='example.com'>My Website</a></p>
<h2></h2>";
$domDocument= new DOMDocument();
$domDocument->loadHTML($paragraph,LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
//getting all nodes with tagname a
$results=$domDocument->getElementsByTagName("a");
foreach($results as $resultantNode)
{
$href=$resultantNode->getAttribute("href");//getting href attribute
$resultantNode->setAttribute("href","mywebsite.com/track/"."yourEncoded:$href");//replacing with the value you want.
}
echo $domDocument->saveHTML();
Related
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();
This is my code :
$patt = "#href=\"(.*?)\"#";
preg_match($patt,$data,$match);
echo $match[1];`
i.e. theres a URL in the HTML code of the page $data
<a href="http://aba.ai/iEU9x">
I want to grab this link above. Thanks
I've currently got a few DB entries which look like this:
1. This is some text http://www.sitehere.com more text
2. Text https://www.anothersite.com text text text
3. http://sitehere.com http://sitehereagain.com
4. Just text here blabla
I am trying to filter those entries while printing them and add infront of all the urls http://anothersite.com/?. Also put the new url destination as link but keep the original url as text:
text text http://sitehere.com text
Until now I've managed to add the http://anothersite.com/? part with the following code:
$result = preg_replace('/\bhttp:\/\/\b/i', 'http://anothersite.com/?http://', $input);
$result = preg_replace('/\bhttps:\/\/\b/i', 'http://anothersite.com/?https://', $input);
But the ahref is not the way I want it. Instead it is:
text text http://anothersite.com/?http://sitehere.com text
PS: I am not looking for a javascript solution :) Thank you!
This following code should work. There are a few large changes I made. The first one is I am using preg_replace_callback instead of preg_replace so I am able to properly encode the URL and have more control over the output. The other change is I'm matching the whole domain so the callback function can insert the URL between the <a> tags and also can add it to the hyperlink.
<?php
$strings = array(
'This is some text http://www.sitehere.com more text',
'Text https://www.anothersite.com text text text',
'http://sitehere.com http://sitehereagain.com',
'Just text here blabla'
);
foreach($strings as $string) {
echo preg_replace_callback("/\b(http(s)?:\/\/[^\s]+)\b/i","updateURL",$string);
echo "\n\n";
}
function updateURL($matches) {
$url = "http://anothersite.com/?url=";
return ''.$matches[1].'';
}
?>
I have the following string:
$linkString="The Following is a link to google <a class='links' href='http://google.com'>
http://google.com
</a>
";
In this string the hypertext of the html link in new line. I want to remove and may be replace all of the link (its html tag and the hypertext) from the string, so I tried the following:
<?php
$linkString="The Following is a link to google <a class='links' href='http://google.com'>
http://google.com
</a>
";
//Remove link tag:
echo preg_replace('/<[^>]*>/','',$linkString);
However, the above example prints out:
The Following is a link to google
http://google.com
This is an online DEMO: http://codepad.org/whw81bwa
I want to know a regex that able to remove all the link (tag and hypertext)
Instead of using regex, make effective use of DOM to do this for you.
$doc = new DOMDocument;
#$doc->loadHTML($html); // load the HTML data
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a') as $tag) {
$tag->parentNode->removeChild($tag);
}
echo $doc->saveHTML();
The following regex solve the issue:
/(?i)<a([^>]+)>(.+?)<\/a>/'
So,
<?php
$linkString="The Following is a link to google <a class='links' href='http://google.com'>
http://google.com
</a>
";
//Remove link tag:
echo preg_replace('/(?i)<a([^>]+)>(.+?)<\/a>/','A Hidden Link',$linkString);
Let's say I submit a form with the message:
Hi! What's up? Click here to check out my website.
How can detect if the string contains <a> tags with PHP, and then add rel="nofollow" to it? So it would change to:
Hi! What's up? Click here to check out my website.
A little speculation of how the code would function?
$string = $_POST['message'];
if (*string contains <a> tags*) {
*add rel="nofollow"*
}
There's always the DOMDocument object.
<?php
$dom = new DOMDocument();
$dom->loadHTML('woo! examples!');
foreach ($dom->getElementsByTagName('a') as $item) {
$item->setAttribute('rel', 'nofollow');
}
echo $dom->saveHTML();
?>