How to replace a URL to an Hyperlink in a String?

PHP

At times you come across a scenario where the string contains an http link and you need to create the hyperlink to the same.

This is can very well achieved using the single line of code.


<?php

$text = "This is http://www.phpcoaching.in site";
echo $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\0\" target=\"_blank\">\0</a>", $text);

?>

Source

How to fetch Twitter latest updates using PHP?

Twitter API

many a times we need to fetch twitter updates on to our business website or elsewhere.

This is can very well achieved using the following lines of code.


<?php
$username='techoguru';
$format='xml';

$tweet=simplexml_load_file("https://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}");

echo $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\0\" target=\"_blank\">\0</a>", $tweet->status[0]->text);
?>

Source

Also if when we need to with our wordpress site


//Set parameter for query string
$username = 'techoguru';
$num = '10';
// Get the data from Twitter JSON API
$json = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=$username&count=$num");
// Decode JSON into array
$data = json_decode($json['body'], true);
//Create unordered list
echo '<ul class="twitter">';
//Loop through all twitter tweets and display the text with the time it was created
foreach($data as $tweets)
{
    $text = $tweets['text'];
    $date = $tweets['created_at'];
    //Make time human readable ie. 6 hours ago
    $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
    echo '<li>'.ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\0\" target=\"_blank\">\0</a>", $text) . ' '. $h_time . '</li>';
}
//Close list
echo '</ul>';

Source

Redirect Naked URL with www using htaccess file

Apache

There is some times requirement to redirect naked URL to URL having “www”.

We have the solutions with our .htaccess file.

The following line of code will help us to achieve the purpose.


### re-direct to www
RewriteCond %{HTTP_HOST} ^phpblog.in
RewriteRule (.*) http://www.phpblog.in/$1 [R=301,L]

How to add favicon icon file to wordpress blog or site?

Wordpress

Today I was thinking of adding a favicon icon for my blog PHP Blog

For making favicon.ico file

  1. Go to http://www.favicon.cc/
  2. Upload image for which you need to create the favicon icon
  3. or you can create your own using the tool
  4. After Uploading the image, the created ico file can be downloaded
  5. Upload to the theme folder

<link rel="shortcut icon" href="&lt;?php bloginfo('stylesheet_directory'); ?&gt;/favicon.ico" />

How to Check MySQL Version from Command Prompt

MySQL

Today I was asked to provide MYSQL version which we have used within our integrated system.

We as a developers are not bothered about the version, I was not aware of it and on my local system I have WAMP installed, so it directly gives the MYSQL version.

So to get MYSQL version from “mysql” command line, I got help from one of the site which asked to use the following line of code


select version ();

This will display the current MYSQL version used within system

How to find no of checkboxes using javascript?

Javascript jQuery

To find no of checkboxes in html using jQuery or javascript

var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  
var cbs = []; //will contain all checkboxes  
var checked = []; //will contain all checked checkboxes  
for (var i = 0; i < inputs.length; i++) {  
  if (inputs[i].type == "checkbox") {  
    cbs.push(inputs[i]);  
    if (inputs[i].checked) {  
      checked.push(inputs[i]);  
    }  
  }  
}  
var nbCbs = cbs.length; //number of checkboxes  
var nbChecked = checked.length; //number of checked checkboxes 

The following code gives no of check boxes present and no of checked.

Using the above code I have written following code

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function chking()
{
/* For Running jQuery */
var cbs = $("input:checkbox"); //find all checkboxes  
var nbCbs = cbs.size(); //the number of checkboxes  
   
var checked = $("input[@type=checkbox]:checked"); //find all checked checkboxes + radio buttons  
var nbChecked = checked.size(); 

/* For Running jQuery */
/* For Running Javascript */

var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  
var cbs = []; //will contain all checkboxes  
var checked = []; //will contain all checked checkboxes  
for (var i = 0; i < inputs.length; i++) {  
  if (inputs[i].type == "checkbox") {  
    cbs.push(inputs[i]);  
    if (inputs[i].checked) {  
      checked.push(inputs[i]);  
    }  
  }  
}  
var nbCbs = cbs.length; //number of checkboxes  
var nbChecked = checked.length; //number of checked checkboxes 

alert(nbCbs);

alert(nbChecked);

/* For Running Javascript */
}
</script>
</head>

<body>
<form method="post" action="">
<input type="checkbox" name="cricket" value="cricket" /> cricket
<input type="checkbox" name="football" value="football" /> football
<input type="checkbox" name="hockey" value="hockey" /> hockey
<input type="checkbox" name="chess" value="chess" /> chess
<input type="checkbox" name="cards" value="cards" /> cards
<input type="button" value="check" onclick="chking();" />
</form>
</body>
</html>

How to repeat a string n times in PHP?

PHP

Question
How to repeat a string n times in PHP?

Answer

<?php
echo str_repeat(" www.phpblog.in ", 3);
?>

Explanation

This function will repeat a particular string 3times and display on the screen

How to define a variable in PHP?

PHP

How to define a variable in PHP?

<?php

define("ABC","Online Training");
echo ABC;//Online Training

?>

Also if I change the value of the variable at later stages what will happen

<?php

define("ABC","Online Training");
echo ABC;//Online Training
............
............
............
define("ABC","Online Training Institute");
echo ABC;//Online Training

?>

Even if we change the value it will display the previously set value only

Page 1 of 3123