php replace links - php

I have been racking my brains over this for hours, I need to pass some html to a function and have it replace the links and the return the html with the replaced links.
<?php
final static public function replace_links($campaign_id, $text) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $text, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
if(substr($match[2], 0, 2) !== '##') { // ignore the placeholders
if(substr($match[2], 0, 6) !== 'mailto') { // ignore email addresses
// $match[2] = link address
// $match[3] = link text
$url = "http://xxx.com/click?campaign_id=$campaign_id&email=##email_address##&next=" . $match[2];
#$text .= str_replace($match[2], $url, $text);
#echo $links . "\n";
preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
}
}
return $text;
}
}
}
?>
When I echo the links it shows all the matched links. Question is how do i return the complete HTML with the replaced links example below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
xxxx
xxxx
xxxx
</body>
</html>
Should become:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
xxxx
xxxx
xxxx
</body>
</html>
Hope this makes sense.
Thanks in advance,
Kyle

Don't reinvent the wheel just use something like this:
http://code.google.com/p/jquery-linkify/
It's really easy i use it as well

I don't know much about preg_replace, but i needed exatcly the same function as you.
Changing the line:
preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
for this:
str_replace($match[0],$url,$text);
seems to do the trick.
I just needed to get the return from this functions, so:
//$text = preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
$text = str_replace($match[0],$url,$text);

Related

How to redirect after fopen() in php

I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).

Ping server every 1 second infinitely till closed?

I'm wanting to set up a loop or maybe a page refresh that pings my server over and over again and tells me the milliseconds.
This is the code I'm using but not sure how to make it keep refreshing and giving me the response live. Can someone show me how to make it live so it constantly updates every 1 second or even every 2 or 3 seconds is fine also. Just need it to be live.
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
Edited with Paul's Code still no luck:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
</body>
</html>
There are two ways I can think of:
1.Setting up refresh attribute in the page header
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
//your php code here
?>
</body>
</html>
2.Use a crontab job to execute this command every second:
w3m http://yourhost/yourPage.php
I think the first solution is closer to what you need.

Extracting images and determining the dimensions of the images

I have a php code that will extract and retrieve all the images in a website. How do I modify the code so that the dimensions(width and height) of the images are shown as well?
This is the php coding:
<?php
$page_title = "MiniCrawler";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php print($page_title) ?></title>
</head>
<body>
<?php
ini_set('display errors',1);
error_reporting(E_ALL|E_STRICT);
Include simple_html_dom.php.
include_once ('simple_html_dom.php');
// Add the url of the site you want to scrape.
$target_url = "http://www.alibaba.com/";
// Let simple_html_dom do its magic:
$html = new simple_html_dom();
$html->load_file($target_url);
// Loop through the page and find everything in the HTML that begins with 'img'
foreach($html->find('img') as $link){
echo $link->src."<br />";
echo '<img src ="'. $link->src.'"><br />';
}
?>
</body>
</html>
Thanks
First you would have to check, if the $link->src string already has the domain name at the beginning:
<?php
if(substr($link->src, 0, 4) == "http"){
// url already complete
$path = $link->src;
}else if(substr($link->src, 0, 1) == "/"){
// path starts absolute
$path = $target_url . $link->src;
}else{
// path starts relative -> http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
}
?>
Then: Request the files dimensions via the getimagesize() function.
<?php
list($width, $height, $type, $attr) = getimagesize($path);
echo '<img src ="'. $link->src.'" width="' . $width . '" height="' . $height . '"><br />';
?>

Logging IP, Browser, Operating System...etc

I'm trying to create a page what will log a plethora of user data for security purposes but am am unsure how best to do so.
Ideally I want all important stats for example their IP, their X-Forward IP, their browser, operating system and any other details I can get.
I've also got problems showing the variables in a php / html page as shown below:
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
$template = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
IP: <?= $ip ?>
IP: [IP]
</body>
</html>';
$template = str_replace('[IP]', $ip, $template);
echo $template;
?>
or
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
Try something simple first:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
For a full list of SERVER superglobal variables, check this link http://php.net/manual/en/reserved.variables.server.php
<?php
$data = $_SERVER;
$IP = $data['REMOTE_ADDR'];
echo "IP address is:" . $IP;
?>
There you go... now look up all the available $_SERVER values and use the ones you need.

PHP file_put_contents new line

I tried use file_put_contents output new page. but I meet some trouble in breaking new line.
<?php
$data ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n';
$data .='<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n';
$data .='<head>\r\n';
$data .='</head>\r\n';
$data .='<body>\r\n';
$data .='<p>put something here</p>\r\n';
$data .='</body>\r\n';
$data .='</html>\r\n';
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
I tried \n or \r\n, they all can not make a new line:
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml" lang="en">\r\n<head>\r\n</head>\r\n<body>\r\n<p>put something here</p>\r\n</body>\r\n</html>\r\n
Using \r or \n in single quotes carries it literally. use double quotes instead like "\r\n"
So one line might become:
$data .= "<head>\r\n";
or
$data .='<head>' . "\r\n";
You are using single-quoted character literals, which don't interpret escape sequences.
Either switch to double-quoted strings or, preferably, use heredoc syntax.
<?php
$data = <<<CONTENTS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
</head>
<body>
<p>put something here</p>
</body>
</html>
CONTENTS;
file_put_contents( dirname(__FILE__) . '/new.php', $data);
?>
But really, why are you writing a hard-coded file? That's really strange.

Categories