#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6

    Help with some functions.


    Can you guys help me out with this. I found this piece of php code and was wondering what I need to do in order to make it work? I noticed it has an $input, $remove_words and $words_array variable. Should these already be defined somewhere?

    Code:
    GENERATE SEO URLS */
    function generate_seo_link($input,$replace = '-',$remove_words = true,$words_array = array())
    {
    	//make it lowercase, remove punctuation, remove multiple/leading/ending spaces
    	$return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));
    
    	//remove words, if not helpful to seo
    	//i like my defaults list in remove_words(), so I wont pass that array
    	if($remove_words) { $return = remove_words($return,$replace,$words_array); }
    
    	//convert the spaces to whatever the user wants
    	//usually a dash or underscore..
    	//...then return the value.
    	return str_replace(' ',$replace,$return);
    }
    
    /* takes an input, scrubs unnecessary words */
    function remove_words($input,$replace,$words_array = array(),$unique_words = true)
    {
    	//separate all words based on spaces
    	$input_array = explode(' ',$input);
    
    	//create the return array
    	$return = array();
    
    	//loops through words, remove bad words, keep good ones
    	foreach($input_array as $word)
    	{
    		//if it's a word we should add...
    		if(!in_array($word,$words_array) && ($unique_words ? !in_array($word,$return) : true))
    		{
    			$return[] = $word;
    		}
    	}
    
    	//return good words separated by dashes
    	return implode($replace,$return);
    }
    What I am trying to do is make an seo friendly url that will contain the month, day, year, and title. Exactly like Wordpress.

    Can any of you share some code that would do this or help me to figure out the above code? In my database I have a row called title and date.

    Lets say I got a title called: This Is My Title
    I would like the url to look like this:
    /2012/10/02/this-is-my-title.html

    Hope that helps
    Last edited by tech0925; October 2nd, 2012 at 04:10 PM.
  2. #2
  3. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    971
    The generate_seo_link will strip the link of bad characters and replace any spaces with "-" or whatever character you want to replace it with. I ran the function using this url: "http://somedomain.com/actions?page=12&blah=false" and got back "httpsomedomaincomactionspage12blahfalse".

    What you can do is pass anything after the ".com/" (there is a word for these particular parameters, but my brain is failing me) to the function, and then add the following lines to it:

    PHP Code:
    $return .= "-".date("Y-m-d"time();
    $return .= "-".$title
    Where $title is either something you pass to the function or that you retrieve from the database within the function. You may need to play with it a little to get your formatting just right. The remove_words function you may or may not use, if you have an array of words that are usually in your links that you want removed, great, but if otherwise you can just set $remove_words to false each time.
    "Those who can make you believe absurdities can make you commit atrocities."
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Thank you for your time. So if I combine the date and title into a variable called $link would I call it like this?

    Code:
    generate_seo_link($link);
    
    echo $link;
    Also, what does the $input in the function do? Should I change that to $link?
    Code:
    generate_seo_link($input,$replace = '-',$remove_words = true,$words_array = array())


    Thanks for the help!
    Last edited by tech0925; October 2nd, 2012 at 10:12 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Ok, I created a db table that contains bad words that I don't want in the link. I then put them into an array on the index.php file called $words_array and the link that I want it to filter is called $link.

    How can I modify the above function file called seourls.php and call it in the index.php file to work properly?? Or can someone show me a good example of a function that will do this using the $words_array to filter out the bad words and spaces in the $link variable.

    These functions just confuse the mess out of me. Thanks
    Last edited by tech0925; October 2nd, 2012 at 11:06 PM.
  8. #5
  9. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    971
    Originally Posted by tech0925
    Ok, I created a db table that contains bad words that I don't want in the link. I then put them into an array on the index.php file called $words_array and the link that I want it to filter is called $link.

    How can I modify the above function file called seourls.php and call it in the index.php file to work properly?? Or can someone show me a good example of a function that will do this using the $words_array to filter out the bad words and spaces in the $link variable.

    These functions just confuse the mess out of me. Thanks
    The $input is the parameter that is being passed to the function, if you look at the function itself, you'll see. It is, as the name suggests, the input that you want SEO'd.

    If you want to call it in index.php, you need to require or include it. You may want to spend some time reading about those particular functions here: [PHPNET="require"]Require[/PHPNET]. You don't need another function to omit words and spaces, the one you have there already does it.

    Comments on this post

    • tech0925 agrees
    "Those who can make you believe absurdities can make you commit atrocities."

IMN logo majestic logo threadwatch logo seochat tools logo