The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Help with some functions.
Discuss Help with some functions. in the PHP Development forum on Dev Shed. Help with some functions. PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 2nd, 2012, 03:13 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 23 m 55 sec
Reputation Power: 5
|
|
|
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.
|

October 2nd, 2012, 04:28 PM
|
 |
We're trapped inside a game!
|
|
Join Date: Jul 2008
Location: Maryland
|
|
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."
|

October 2nd, 2012, 04:54 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 23 m 55 sec
Reputation Power: 5
|
|
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.
|

October 2nd, 2012, 08:39 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 23 m 55 sec
Reputation Power: 5
|
|
|
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.
|

October 3rd, 2012, 12:43 PM
|
 |
We're trapped inside a game!
|
|
Join Date: Jul 2008
Location: Maryland
|
|
Quote: | 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: Require. You don't need another function to omit words and spaces, the one you have there already does it.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|