how can i use a function in php

Discussion in 'PHP' started by baris22, Oct 25, 2008.

  1. #1
    hello all,

    I managed to make a function but i do not know how to use it if i get the inf. from database.

    here is my function
    
    
    function safename($title)
    
    
    PHP:
    I want to use it in here

    
    
    <?=$item[title]?>.
    
    
    PHP:
    
    
    <?php
    while($item = mysql_fetch_array($portfolio))
    {
    ?>
    <a href="/links/<?=$item[id]?>/<?=$item[title]?>.html" class="link2"><?=$item[title]?></a>
    <?php
    }
    ?>
                
    
    PHP:
    any idea?

    thanks
     
    baris22, Oct 25, 2008 IP
  2. kkrawal

    kkrawal Peon

    Messages:
    235
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I know, tell me the table name, and field name and sql string to be used in the function.
    pm me
     
    kkrawal, Oct 25, 2008 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    this is the function

    
    
    function safename($title){
    // replaces every unwanted character form a string with - ;
    		$title = str_replace("&", "and", $title);
    		$arrStupid = array('feat.', 'feat', '.com', '(tm)', ' ', '*', "'s",  '"', ",", ":", ";", "@", "#", "(", ")", "?", "!", "_",
    							 "$","+", "=", "|", "'", '/', "~", "`s", "`", "\\", "^", "[","]","{", "}", "<", ">", "%", "â„¢");
    
    		$title = htmlentities($title);
      		$title = preg_replace('/&([a-zA-Z])(.*?);/','$1',$title); // get rid of bogus characters
    		$title = strtolower("$title");
    		$title = str_replace(".", "", $title);
    		$title = str_replace($arrStupid, "-", $title);
    		$flag = 1;
    			while($flag){ 
      			  $newtitle = str_replace("--","-",$title);
    				if($title != $newtitle) { 
    					$flag = 1;
    				 }
    				else $flag = 0;
     			  $title = $newtitle;
    			}
    		$len = strlen($title);
    		if($title[$len-1] == "-") {
    			$title = substr($title, 0, $len-1);
    		}
    		return $title;
    }
    
    
    PHP:
    My table is: files
    my field is: title

    my sql

    
    
    while($item = mysql_fetch_array($portfolio)){
    
    
    PHP:

    I use:

    
    
    <?=$item[title]?>
    
    
    PHP:
    to get the title row at the moment

    Thanks
     
    baris22, Oct 25, 2008 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?=safename($item[title])?>.
     
    Kyosys, Oct 25, 2008 IP
    baris22 likes this.
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    thank you.

    it worked perfect
     
    baris22, Oct 25, 2008 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    edit : answered already
     
    bartolay13, Oct 25, 2008 IP
  7. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no problem
     
    Kyosys, Oct 25, 2008 IP