techno surprises

September 4, 2008

C# source code for strip_tags in PHP

Filed under: c, c sharp, csharp, php, strip_tags — Tags: , , , , , , , — Mustafa Turan @ 11:52 pm

Source to download: http://mustafaturan.net/download/lectures/csharp/strip_tags_csharp.txt

// as a namespace do not forget to add: using System.Text.RegularExpressions;
static string strip_tags(string str, string allowed_tags)
{
/*
// Coder: Mustafa Turan
// Date: 05.09.2008
// Contact:
// http://mustafaturan.net/
// https://mustafaturan.wordpress.com/
// wm [ #at# ] mustafaturan.net
// Licence: GNU and MIT Licence
// EXAMPLES
// —>     function call1: strip_tags(“<a href=\”asdadsadsad.html\”>doctor</a> <p>pirasa</p> <img src=\”asd.jpg\” /> <h1>hey you</h1>”, “<a>,<p>,<img />”)
// —>     result: <a href=””>doctor</a> <p>pirasa</p> <img src=”asd.jpg” /> hey you
// —>     function call2: strip_tags(“<a href=\”asdadsadsad.html\”>doctor</a> <p>pirasa</p> <img src=\”asd.jpg\” /> <h1>hey you</h1>”, “”)
// —>     result: doctor pirasa hey you
*/

// START
// pattern for getting all tags
string pattern_for_all_tags = “</?[^><]+>”;

// pattern for allowed tags
string allowed_patterns = “”;
if(allowed_tags!=””){
// get allowed tags if any exists
Regex r = new Regex(“[\\/<> ]+”);
allowed_tags = r.Replace(allowed_tags,””);
string[] allowed_tags_array = allowed_tags.Split(‘,’);
foreach (string s in allowed_tags_array)
{
if (s == “”) continue;
// Definin patterns
string p_1 = “<” + s + ” [^><]*>$”;
string p_2 = “<” + s + “>”;
string p_3 = “</” + s + “>”;
if(allowed_patterns!=””)
allowed_patterns += “|”;
allowed_patterns += p_1 + “|” + p_2 + “|” + p_3;
}
}

// Get all html tags included on string
Regex strip_tags = new Regex(pattern_for_all_tags);
MatchCollection all_tags_matched = strip_tags.Matches(str);

if (allowed_patterns != “”)
foreach (Match m in all_tags_matched)
{
Regex r_1 = new Regex(allowed_patterns);
Match m_1 = r_1.Match(m.Value);
if (!m_1.Success)
{
// if not allowed replace it
str = str.Replace(m.Value, “”);
}
}
else
// if not allow anyone replace all
str = strip_tags.Replace(str, “”);
return str;
}

January 23, 2008

PHP – MSSQL to XML

Filed under: code, column, connect, connection string, mssql, name, php, php mssql, row, sample, software, xml — Mustafa Turan @ 4:04 pm

These piece of PHP code is help to understand;

  • How to get column names from MSSQL with PHP?
  • How to get related row attributes from MSSQL with PHP?
  • And how to convert MSSQL tables to XML data store type?

Here is the source code, it is completely free and usable for any purposes.

<?php

// Coder Mustafa Turan

// https://mustafaturan.wordpress.com/
// get query from URL
$query    = urldecode($_GET[‘q’]);
if(empty($_GET[‘q’])) exit();
echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>”;
    /*
    sample query:
    $query = “SELECT * FROM table_name”;
    */

    // connection open to MSSQL
    $link = mssql_connect(‘hostname’, ‘username’, ‘password’);
    mssql_select_db(‘dbname’, $link);

    // get the query result
    $result = mssql_query($query, $link);
    $i = 0;

    // get all columns readed from query
    while ($column = mssql_fetch_field ($result)){
        $columnx[$i] = $column->name;
        $i++;
    }
    $column = null;

    // read all rows with its column name
    while (($row = mssql_fetch_array($result, MSSQL_BOTH)))
    {
        echo “\n <new_row>”;
        foreach($columnx as $C){
            // $C                : column name
            // $row[$C]            : it give the current rows’ column variable
            // sample output    : <column_name>column_content</column_name>
            echo “\n  <” . $C . “>” . $row[$C] . “</” . $C . “>”;
        }
        echo “\n </new_row>”;
    }

    // close the current connection
    mssql_free_result($result);
    mssql_close($link);
echo “\n</xml>”;
?>

December 6, 2007

Random Team Matching with php

Filed under: php, programming — Tags: , , , , , , , , , , , — Mustafa Turan @ 5:26 pm

echo "start\n\n"; $a1 = Array('Chealsea', 'Arsenal', 'Manchester', 'Liverpool');$a2 = Array('Ato', 'Ozi', 'Mustafa', 'Emo');
$b = "0123";

$c = "0123";   for($i=0;$i<(sizeof($a1)-1);$i++) {

if($i!=0){

$b	= str_replace($s1,"",$b);

$c	= str_replace($s2,"",$c);

}

$s1	= substr(str_shuffle($b), 0, 1);

$s2	= substr(str_shuffle($c), 0, 1);

echo $a1[$s1] . ":" . $a2[$s2] . "\n";

}

echo "\n\nend";

July 19, 2007

Translating domain_name to ip_address

Domain names can be translated its related ip addresses.

In windows getting ip_address from domain_name

  1. Click on START menu
  2. Click on RUN and type ‘ cmd
  3. then write ping yahoo.com
  4. and get the ip addres of yahoo

In any UNIX based system getting ip_address from domain_name

  1. Run SHELL client of operating system
  2. type command : ping yahoo.com
  3. and get the ip addres of yahoo

In PHP(web based programming language) getting ip_address from domain_name

<?php
$ip = gethostbyname(‘yahoo.com’);

echo $ip;
?>

Blog at WordPress.com.