Jul 30
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo "The string $testcase consists of all letters or digits.\n";
} else {
echo "The string $testcase does not consist of all letters or digits.\n";
}
}
?>
More native functions can be found here
http://in2.php.net/ctype_alnum
Jun 15
Here is a simple code in php that will split a string…
<?php
$str = “Hello Friend”;
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
The above example will output:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
Jun 09
I searched php.net for array search function but I did n’t find any so I thought about writing my own function for it
Here is a preview of the function:
Suppose if there is an array of names -John,Charles,Richard,Richard and Bill.
You want to find how many times Richard is in the array and in which array placethe name”Richard” is…(Richard is displayed 2 times in the array and it is 2,3)
Here is the array function which will help you to find these attributes
<?
function arraysearch($search,$array)
{
$cc=count($array);
for($i=0;$i<$cc;$i++)
{
$d1=$array[$i];
if($d1==$search)
{
$array1=($d1.",".$array1);
$array2=($i.",".$array2);
}
}
echo"<pre>";
/*echo $array1;
echo"<BR>";
echo $array2;
echo"<BR>";
*/
$array1=explode(",",$array1);
$c1=count($array1);
//print_r($array1);
unset($array1[$c1-1]);
$array2=explode(",",$array2);
//print_r($array2);
$c2=count($array2);
unset($array2[$c2-1]);
$c2=count($array2);
//echo $c1;
if(!search)
{
}
else
{
echo "No of $search is $c2";
}
//return($array1);
echo"<BR>";
$array2=implode(",",$array2);
return($array2);
}
$q = stripslashes ($HTTP_GET_VARS['q']);
$c = stripslashes ($HTTP_GET_VARS['c']);
$c=explode(",",$c);
$a1= arraysearch($q,$c);
//print_r($a1);
echo"</pre>";
?>
Jun 08
To find the string length in php
just use
<?
$a=”shanmugam”;
$b=strlen($a);
echo $b; //will display length of the string
?>
Output:
9
May 23
Get the real ip address of the website by php
The below code will help you to find IP Address of the website
<?php
$ip = gethostbyname(”www.youtube.com“);
echo $ip;
?>
May 19
Here is the php code prepared by me to get all the links in a web page
//getting data from the url
$url='http://www.rmmfa2004.org';
$data=@file_get_contents($url);
//extracting the links
preg_match_all("/(href)\s*=\s*[\'\"]?(([[a-z]{3,5}:\/\/(([.a-zA-Z0-9-])+(:[0-9]+)*))*([+:%\/\?~=&;\\\(\),._ a-zA-Z0-9-]*))(#[.a-zA-Z0-9-]*)?[\'\" ]?(\s*rel\s*=\s*[\'\"]?(nofollow)[\'\"]?)?/i", $data, $matches);
$match1= $matches[2];
$match1=split("\r",$matches);
$linkss2 = array_unique($matches[2]);
$links= ($linkss2);
$links1=$links;
echo $linkss2[133];
//array to print links
echo"
";
print_r($matches[2]);
echo"
“;
//stripping slashes from the url
$patternb=”/\/$/i”;
$replace=”";
$url=preg_replace($patternb,$replace,$url);
echo $url;
echo “
“;
$linkss2 = array_unique($links);
$links=$linkss2;
//counting the no of links
$link2=array($links2);
$count=sizeof($matches[2]);
//Generating links
for($i=0;$i<$count;$i++)
{
$links1=str_split($links[$i]);
$links2= $links1[0].$links1[1].$links1[2].$links1[3];
$links3='http';
if(($links2)==$links3)
{
$link= $links[$i];
}
else
{
if(($links1[0]!='/'))
{
$link= $url.'/'.$links[$i];
//To strip session ids
$pattern="/\?(.*)/i";
preg_match($pattern,$link,$matchess);
//echo $matches[1];
$length=strlen($matchess[1]);
//echo $length;
if($length>17)
{
$patterna=”/\?$matchess[0]/i”;
$replace=”\t”;
$link= preg_replace($patterna,$replace,$link);
}
else
{
}
}
else
{
$link=$url.$links[$i];
}
}
//printing the link
echo $link;
$link1=$link1.” “.$link;
echo”
“;
}
//return($link1);
$link1=explode(” “,$link1);
$link1 = array_unique($link1);
print_r($link1);
echo”
“;
?>
May 08
Sending a HTML mail by Php
Here is code below to send a HTML mail in Php
// multiple recipients
$to = ’shandfg@rediffmail.com’;
// subject
$subject = ‘Signature link from DP’;
// message
$message = ‘
<html>
<head>
<title>Signature link from DP</title>
</head>
<body>
<p>A new thread for Signature link has been started</p>
</body>
</html>
‘;
// To send HTML mail, the Content-type header must be set
$headers = ‘MIME-Version: 1.0′ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;
// Additional headers
$headers .= ‘To: shangce3@rediffmail.com’ . “\r\n”;
$headers .= ‘From: Abdul Kalam<apj@abdulkalam.com>’ . “\r\n”;
//$headers .= ‘Cc: birthdayarchive@example.com’ . “\r\n”;
//$headers .= ‘Bcc: birthdaycheck@example.com’ . “\r\n”;
// Mail it
mail($to, $subject, $message, $headers);
May 08
This code is used to get list of all the links in a HTML page!
$file=file_get_contents(”http://groups.google.com/group/filtering-in-Iran/t/ea3859b0a8aefb88″);
$file = preg_replace(’#<\s*script[^>]*?>.*?<\s*/\s*script\s*>#si’, “”, $file);
//echo $file;
preg_match_all(”/(href)\s*=\s*[\’\”]?(([[a-z]{3,5}:\/\/(([.a-zA-Z0-9-])+(:[0-9]+)*))*([+:%\/\?~=&;\\\(\),._ a-zA-Z0-9-]*))(#[.a-zA-Z0-9-]*)?[\’\” ]?(\s*rel\s*=\s*[\’\”]?(nofollow)[\’\”]?)?/i”, $file, $matches);
$match1= $matches[2];
echo”<pre>”;
Apr 27
Here is a tutorial which will explain how to pass Javascript variables to Php.This tutorial works only for files under the same server
Aim:To pass document.referrer variable to Php and to write the referrer variable on a text file;
First let us create two file called 1a.html and 2a.html
1a.html will contain a link to 2a.html
and 2a.html will contain just two javascript calls one will call a file called xml1.js
Here is the source of 1a.html
<a href=’http://www.ramanean.com/java/2a.html’>2</a>
Source of 2a.html
<html>
<head>
</head>
<body>
<script src=”xml1.js”></script>
<script type=”text/javascript”>
change1();
</script>
Here First we are calling xml.js javascript and then calling change1() function in it
Source of xml1.js
var xmlHttp
function change1()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert (”Your browser does not support AJAX!”);
return;
}
var shan=document.referrer;
var url=”78.php”;
url=url+”?q=”+shan;
xmlHttp.open(”GET”,url,true);
xmlHttp.send(null);
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
In the above function function GetXmlHttpObject() is nothing but creating Xmlhttpobject by which get function will be routed (AJAX).Do n’t worry about that if you need to more about there go to http://www.w3schools.com/ajax/ajax_source.asp.
Now let us go to the second line ,Here we are declaring change1 function and then we checking whether browser supports xml if not browser will display an error message indicating that “your Browser does n’t support AJAX”
Now let me explain what these four lines contain
var s =document.referrer; // javascript variable you are going to pass
var url=”78.php”; //Url aof the Php script you are going to pass javascript varaibles
url=url+”?q=”+shan;// adding javascript variables to the url
xmlHttp.open(”GET”,url,true); //callingthe php script
xmlHttp.send(null);//calling the php script
The php file is like this
<?php
$q=$_GET[”q”];//Getting the javascript variable
$file=file_put_contents(’ip1.txt’,$q);//writing the variable to file
?>
by this you can pass variables to a php script and from php you can insert any data into your database;
Apr 23
I just found a new way of calling a php file
You have heard about this
<script type=”text/javascript” src=”java2.js”>
</script>
The good thing is that you can also call a php file using this SRC attribute javascript just replace “java2.js” with “java2.php” like this
<script type=”text/javascript” src=”java2.php”>
</script>
The good thing is you do n’t have to mess with the design of your site with the php code as you can just call by javascript and it can be also used on plain html pages.For example ,I have a free webhosting account for my site www.freewebspace.co.nr ,to know the ip address of the visitors to my site , I will simply call my php file ip1.php which resides on this site.
Something like this
<script type=”text/javascript” src=”http://www.ramanean.com/ip1.php”>
</script>
And the only one disadvantage you have is you can’t find the referer to your site by calling the php file you have to find it by javascript only ,if you intend to find referer toyour plain HTML page.