INDEX ADBRITE
EXTRACT LINK AND NAME FROM HTML USING PHP CODE SNIPPET
Posted by anonymous in PHP CODE SNIPPET on Tuesday, August 31, 2010
some times you may need to extract the name and link from html link.
here is a simple php code snippet to do the job.
it simply extract the link and name and save it into array.
here is the code...
<?php
function extract_linkdata($htmllink){
$data=array();
//array for storing url and name
$var=array();
array_push($var,$htmllink);
//ALGORITHM FOR EXTRACTING LINK
$link_start=stripos($var[0],'"')+1;$one=substr($var[0],$link_start);
$link_end=stripos($one,'"');
$extracted_link=substr($one,0,$link_end);
$data[url]=$extracted_link;
//ALGORITHM FOR EXTRACTING NAME
$name_start=stripos($var[0],'>')+1;
$one=substr($var[0],$name_start);
$name_end=stripos($one,'<');
$extracted_name=substr($one,0,$name_end);
//saving the name into array
$data[title]=$extracted_name;
return $data;
}
?>
how to use the function:
<?php
$link_and_name=extract_linkdata('<a href="www.iloveutech.blogspot.com">iLOVEuTECH</a>');//use any html link as parameter
//now access the link by this way or do anything with the link
echo $link_and_name[url];
//access the link name
echo $link_and_name[title];
?>
here is a simple php code snippet to do the job.
it simply extract the link and name and save it into array.
here is the code...
<?php
function extract_linkdata($htmllink){
$data=array();
//array for storing url and name
$var=array();
array_push($var,$htmllink);
//ALGORITHM FOR EXTRACTING LINK
$link_start=stripos($var[0],'"')+1;$one=substr($var[0],$link_start);
$link_end=stripos($one,'"');
$extracted_link=substr($one,0,$link_end);
$data[url]=$extracted_link;
//ALGORITHM FOR EXTRACTING NAME
$name_start=stripos($var[0],'>')+1;
$one=substr($var[0],$name_start);
$name_end=stripos($one,'<');
$extracted_name=substr($one,0,$name_end);
//saving the name into array
$data[title]=$extracted_name;
return $data;
}
?>
how to use the function:
<?php
$link_and_name=extract_linkdata('<a href="www.iloveutech.blogspot.com">iLOVEuTECH</a>');//use any html link as parameter
//now access the link by this way or do anything with the link
echo $link_and_name[url];
//access the link name
echo $link_and_name[title];
?>
This entry was posted on Tuesday, August 31, 2010 at 11:29 AM and is filed under PHP CODE SNIPPET. You can follow any responses to this entry through the RSS 2.0. You can leave a response.
- No comments yet.