How to replace plain URLs with links in JavaScript or PHP?

Hello Friends

If you want to convert plain text in to URLs in JavaScript or PHP. This is good solution for you.

In PHP :

public function makeLinks($str)
{
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    $urls = array();
    $urlsToReplace = array();
    if(preg_match_all($reg_exUrl, $str, $urls)) {
        $numOfMatches = count($urls[0]);
        $numOfUrlsToReplace = 0;
        for($i=0; $i<$numOfMatches; $i++) {
            $alreadyAdded = false;
            $numOfUrlsToReplace = count($urlsToReplace);
            for($j=0; $j<$numOfUrlsToReplace; $j++) {
                if($urlsToReplace[$j] == $urls[0][$i]) {
                    $alreadyAdded = true;
                }
            }
            if(!$alreadyAdded) {
                array_push($urlsToReplace, $urls[0][$i]);
            }
        }
        $numOfUrlsToReplace = count($urlsToReplace);
        for($i=0; $i<$numOfUrlsToReplace; $i++) {
            $str = str_replace($urlsToReplace[$i], "<a target='_balnk' href=\"".$urlsToReplace[$i]."\">".$urlsToReplace[$i]."</a> ", $str);
        }
        return $str;
    } else {
        return $str;
    }
}

In JavaScript

function makeLinks(text) {
 var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
 return text.replace(exp,"<a target='_blank' href='$1'>$1</a>");
}

Hope it helps.

Advertisements

Move (Copy) Div From One Place To Another Using jQuery

Hello Friends if you want to copy or move the data from one div to onther div you can do it easily with jquery.I can do it by creating new HTML and append it to Active records list. But i came to know about Clone functionality of jQuery, Which is best thing to move or copy any html element.

here are two possibility in Clone function of jQuery. If you want to keep the Div at their place and create the same Div at some other place than you can go for Clone. If you want to Move the Div from one place to another than use appendTo. Look at below syntax.

$(“div#source_div_id”).appendTo(“div#destination_div_id”);

Find Number of Child Elements (Length) Using jQuery

Hi Friends, Some days ago I was working on one task in which I was trying to find number of child elements by id or class of parent element by jQuery. I searched about it and found solution for it, So I thought it is worth to share on my wesbite for users, So it can be helpful for anyone and save time for someone.

So below I am going to show to show you can find number of child elements using jQuery.

$(“div#parent_div”).children().length

So by above code you can find number of child elements by id of parent element. In above code id of parent element is “parent_div” , So if you will use this code you will get number of child elements inside this parent.

So by above code you can find number of child elements by id of parent element. In above code id of parent element is “parent_div” , So if you will use this code you will get number of child elements inside this parent

Jquery Post

Contains common Jquery code

* Display jQuery version

alert(jQuery.fn.jquery);

* Find how many check boxes have been checked

var count = $(‘input[type=checkbox]:checked’).length;

* Get ID, class of an element

var element_id = $(this).attr(‘id’); or element_id = this.id;
var class = $(this).attr(‘class’); or class = this.class;

* Set the attribute of an element

$(this).attr(‘class’, ‘new-class’);
$(this).attr(‘title’, ‘new-title’);

* Set multiple attributes for an element

$(‘this’).attr({
class: ‘new-class’,
title: ‘new-title’
});

* Checking and Un-checking a check-box

this.checked = false; //un-check
this.checked = true; //check

* Loop through all check-boxes and find if they are checked

$(‘input[type=checkbox]’).each(function () {
var is_checked = (this.checked ? “1” : “0”);
});

* Loop through all radio buttons in a form

$(“#form-id”).find(‘:radio’).each(function() {
radio_id = $(this).attr(‘id’);
radio_name = $(this).attr(‘name’);
});

* PHP like explode and implode functions in Javascript

//explode
var my_string = “abc-def-ghi-jkl”;
var my_array = [];
my_array = my_string.split(“-“);

//implode
var my_array = new Array(‘abc’, ‘def’, ‘ghi’, ‘jkl’);
var my_string = “”;
my_string = my_array.join(“-“);

# Disable all radios of a group

$(‘input:radio[name=radio-group-name]’).attr(‘disabled’, true);

# Get the value of the selected radio button in a group

$(‘input:radio[name=radio-group-name]’).val();

# Disable all input elements in a form (input, textarea, select and button)

$(“#form-id :input”).attr(“disabled”, true);

# Disable only input elements of a form

$(“#form-id input”).attr(“disabled”, true);

# Get the current page url

var current_page_url = window.location.pathname;